MENU
String
A string is basically a sequence of characters. A string variable can be declared with a string literal or the String object.
A string literal is enclosed within "...", '...', or `...`. The first two forms are interchangeable. They can span multiples by using a \ at the end of lines.
ECMAScript 6 introduces the third form of strings, template literals, which are enclosed within `...`. They can interpolate expressions within. They can also span multiple lines.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var a='123'; console.log(a); // 123
var b="a'b'c"; console.log(b); // a'b'c
var c=`${a}`; console.log(c); // 123
var c=`${a+b}`; console.log(c); // 123a'b'c
console.log("hello \
world"); // hello
world
console.log(`hello
world`); // hello //
world
console.log(`${3+4}-eleven`); // 7-eleven
</script></body><html>
A template string can be passed to a function without the parentheses, as a tagged template string.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
function f(strings, ...values) {
console.log(strings[0]); // a
console.log(strings[1]); // bc
console.log(strings[2]); // d
console.log(values[0]); // 42
console.log(values[1]); // 999}f `a${ 42 }bc${999}d`;
</script></body><html>
To silence errors caused by invalid Unicode escape sequence, enclose the Unicode within {}.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
console.log("\u9999"); // 香//console.log("\u55"); // SyntaxError: Invalid Unicode console.log("\u{55}"); // U: no error if tagged with {}function tag(s){
console.log(s[0]); // undefined
console.log(s[1]); // undefined
console.log(s.raw[0]); // \u55 \u{55}}tag `\u55 \u{55}`;
</script></body><html>
Note that a string is also an iterable.