Four new usages of strings in Javascript6 sharing _javascript tips

Source: Internet
Author: User

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
  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);

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 ')
  > ' Hello '. EndsWith (' Ello ')
  > ' Hello '. Includes (' Ell ')

These methods have an optional second parameter that indicates the starting position of the search:

  > ' Hello '. StartsWith (' Ello ', 1)
  > ' Hello '. EndsWith (' hell ', 4)
  > ' Hello '. Includes (' ell ', 1)
  > ' Hello '. Includes (' ell ', 2)

repeat()method to duplicate a string:

  > ' Doo ' repeat (3)
  ' doo doo doo '


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.