Basic info and string concatenation
suggest changeStrings in JavaScript can be enclosed in single quotes ('hello'), double quotes ("Hello"), and, since ES2015, template literals (backticks: `hello`).
const hello = "Hello";
const world = 'world';
const helloW = `Hello World`; // ES2015 / ES6
Strings can be created from other types using the String() function.
const intString = String(32); // "32"
const booleanString = String(true); // "true"
const nullString = String(null); // "null"
Or, toString() can be used to convert Numbers, Booleans or Objects to Strings.
const intString = (5232).toString(); // "5232"
const booleanString = (false).toString(); // "false"
const objString = ({}).toString(); // "[object Object]"
Strings can also be created with String.fromCharCode.
String.fromCharCode(104,101,108,108,111) //"hello"
Creating a string with new String is allowed, but it is not recommended. The result is an object, not a primitive string.
const objectString = new String("Yes, I am a String object");
typeof objectString;//"object"
typeof objectString.valueOf();//"string"
Concatenating Strings
String concatenation can be done with +, or with concat() on String.prototype. + also converts non-string operands to strings. concat converts its arguments the same way.
const foo = "Foo";
const bar = "Bar";
console.log(foo + bar); // => "FooBar"
console.log(foo + " " + bar); // => "Foo Bar"
foo.concat(bar) // => "FooBar"
"a".concat("b", " ", "d") // => "ab d"
Strings can be concatenated with non-string variables but will type-convert the non-string variables into strings.
const string = "string";
const number = 32;
const boolean = true;
console.log(string + number + boolean); // "string32true"
String Templates
Strings can be created using template literals (backticks) hello.
const greeting = `Hello`;
With template literals, you can do string interpolation using ${variable} inside template literals:
const place = `World`;
const greet = `Hello ${place}!`
console.log(greet); // "Hello World!"
You can use String.raw to get backslashes to be in the string without modification.
`a\\b` // = a\b
String.raw`a\\b` // = a\\b