"string".endsWith("ing", endPosition = arg1.length) true > "string".includes("rin..." />

New String Features in ES6

New string methods:

    > "string".startsWith("str", position = 0)
    true
    > "string".endsWith("ing", endPosition = arg1.length)
    true
    > "string".includes("rin", position = 0)
    true
    > "string".repeat(3)
    "stringstringstring"
    

Each of these methods has an optional second parameter, which specifies where the string to be searched starts or ends:

    > "string".startsWith("tri", 1)
    true
    > "string".endsWith("in", 5)
    true
    > "string".includes("tri", 1)
    true
    > "string".includes("tri", 2)
    false
    

ES6 has a new type of string literal called a template literal:

    const first = 'Neil';
    const last = 'Thawani';
    
    console.log(`Hello ${first} ${last}.`);
    

Template literals can also be used to create multi-line strings.

    const multiLine = `This
    is
    a
    multi-line
    string.`;
    

Template literals are also 'raw' if you prefix them with the tag String.raw.

    const str = String.raw`Not a newline: \
    `;
    console.log(str === 'Not a newline: \\\
    '); // true
    

Escape characters are not interpreted.

Strings are iterable. You can use for of to iterate over their characters.

    > for (const ch of 'abc') { console.log(ch); }
    a
    b
    c
    

You can use the spread operator to turn strings into Arrays.

    > const chars = [...'abc'];
    ['a', 'b', 'c']
    

Source: Exploring ES6 by Dr. Axel Rauschmayer.

Published March 14, 2016