KandZ – Tuts

We like to help…!

JavaScript 6 🧬 escape characters and template literals

a - escape characters
escape characters are used to represent special characters in strings
example = "This is a backslash: \\"; → This is a backslash: \
example = 'He said, \'Hello!\''; → He said, 'Hello!'
example = "She said, \"Hi there!\""; → She said, "Hi there!"
example = "Line 1\nLine 2"; → Line 1
Line 2
example = "This is an octal escape: \101 (\u0041 in hex)"; → This is an octal escape: A (Unicode 0x0041)
example = "This is a hex escape: \x41 (\u0041 in octal)"; → This is a hex escape: A (Unicode 0x0041)
example = "This is a Unicode escape: \u03A9 (Omega symbol)"; → This is a Unicode escape: Ω (Greek Capital Letter Omega)

b - template literals
template literals provide a new way to work with strings
They allow to embed expressions inside string literals using `
This makes it easier to create multi-line strings and perform string interpolation.
Template Literals, introduced in ES6 (ECMAScript 2015)
let greeting = `Hello, ${name}!`; → Basic Usage
let message = `
This is a
multi-line string.` → Multi-Line Strings
`The sum of ${a} and ${b} is ${a + b}.` → Expression Interpolation

Leave a Reply