Working with strings

ES6/ES7/ES8 provides new ways of creating strings and adds new properties to the global String object and to its instances to make working with strings easier. Strings in JavaScript lacked features and capabilities when compared with programming languages such as Python and Ruby; therefore, ES6 enhanced strings to change that.

Before we get into new string features, let's revise JavaScript's internal character encoding and escape sequences. In the Unicode character set, every character is represented by a base 10 decimal number called a code point. A code unit is a fixed number of bits in memory to store a code point. An encoding schema determines the length of code unit. A code unit is 8 bits if the UTF-8 encoding schema is used or 16 bits if the UTF-16 encoding schema is used. If a code point doesn't fit in a code unit, it is split into multiple code units, that is, multiple characters in a sequence representing a single character.

JavaScript interpreters by default interpret JavaScript source code as a sequence of UTF-16 code units. If the source code is written in the UTF-8 encoding schema then there are various ways to tell the JavaScript interpreter to interpret it as a sequence of UTF-8 code units. JavaScript strings are always a sequence of UTF-16 code points.

Any Unicode character with a code point less than 65,536 can be escaped in a JavaScript string or source code using the hexadecimal value of its code point, prefixed with u. Escapes are six characters long. They require exactly four characters following u. If the hexadecimal character code is only one, two, or three characters long, you'll need to pad it with leading zeroes. Here is an example to demonstrate this:

const u0061 = "u0061u0062u0063";
console.log(a); //Output is "abc"
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.94.173