String Basics
Strings in JavaScript are sequences of Unicode (UTF-16) characters.
const greeting = "Hello ๐";
console.log(greeting);
Delimiters are single or double quotes:
const greeting = "Hello ๐";
const congrats = "Congratulations ๐";
You can also use template literals, using the backtick character ```.
const greeting = `Hello ๐`;
Backtick delimiters are useful for multiline strings and embedded expressions:
const name = "Ali";
const greeting = `**********
Hello ${name}!
**********`;
console.log(greeting);
Notice the ${} syntax. It embeds the value of the name variable in the string.
You can call methods and properties available in the String wrapper object directly on a primitive string value.
const name = "Ali";
console.log(name.length); // 3
console.log(name.charAt(1)); // l
console.log(name.toUpperCase()); // ALI
The statements above work because JavaScript automatically wraps the primitive string in its wrapper object type String.
Since ECMAScript 5, you can access string characters the same way you access array elements, using square bracket notation:
const animal = "cat";
console.log(animal[0], animal[1], animal[2]); // c a t
But you cannot use the square bracket notation to modify the string:
const animal = "cat";
animal[0] = "b";
console.log(animal); // cat