Here are four new uses of strings in JAVASCRIPT6:
A new representation method for Unicode characters
Unicode characters are usually 21 bit, while ordinary JavaScript characters (most) are 16bit and can be encoded into UTF-16. Characters exceeding 16bit are required to be represented in 2 regular characters.
For example, the following code will output a Unicode small rocket character (' \ud83d\ude80 ') that you can try in the browser console:
Console.log (' \ud83d\ude80 ');
In ECMAScript 6, you can use the new presentation method, which is more concise:
Console.log (' \u{1f680} ');
Two, multiline string definitions and template strings
The template string provides three useful syntax features.
First, the template string supports embedded string variables:
let-i = ' Jane ';
Let-last = ' Doe ';
Console.log (' Hello ${first} ${last}! ');
Hello Jane doe!
Second, the template string supports the direct definition of multiple-line strings:
Let MultiLine = ' It is
a string with
multiple
lines ';
Third, if you String.raw
prefix the string, the string will remain in its original state. The backslash (\) does not represent escape, and other professional characters, such as \ n, will not be escaped:
Let raw = String.raw ' not a newline: \ n ';
Console.log (Raw = = ' Not a newline: \\n '); True
Third, loop traversal string
Strings can traverse loops, and you can use for-of
each character in the loop string:
for (Let ch ' abc ') {
console.log (ch);
}
Output:
//a
//b
//C
Also, you can split a string into a character array using the split character (...):
Let chars = [... ' abc '];
[' A ', ' B ', ' C ']
Four, string contains the judgment and duplicate string
There are three new ways to check whether a string contains another string:
> ' Hello '. StartsWith (' hell ')
true
> ' Hello '. EndsWith (' Ello ')
true
> ' Hello '. Includes (' Ell ')
true
These methods have an optional second parameter that indicates the starting position of the search:
> ' Hello '. StartsWith (' Ello ', 1)
true
> ' Hello '. EndsWith (' hell ', 4)
true
> ' Hello '. Includes (' ell ', 1)
true
> ' Hello '. Includes (' ell ', 2)
false
repeat()
method to duplicate a string:
> ' Doo ' repeat (3)
' doo doo doo '
Summarize
This is the four new usage of the string in Javascript6, has everybody learned it? I hope this article can be helpful to everyone, if you have any questions, you can message exchange.