Strings & Templates
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Strings & Templates.
JavaScript Strings
A JavaScript string is zero or more characters written inside quotes. Strings are used for storing and manipulating text.
1. Quotes and Strings
A JavaScript string can be written with single or double quotes:
let carName1 = "Volvo XC60";
let carName2 = 'Volvo XC60';You can use quotes inside a string, as long as they don't match the quotes surrounding the string:
let answer1 = "It's alright";
let answer2 = "He is called 'Johnny'";
let answer3 = 'He is called "Johnny"';2. String Length
To find the length of a string, use the built-in
length property:let text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let length = text.length;3. Escape Characters
Because strings must be written within quotes, JavaScript will misunderstand this string:
// This will error
let res = "We are the so-called "Vikings" from the north.";To solve this problem, you can use the backslash escape character. The backslash (
\) escape character turns special characters into string characters:| Code | Result | Description |
|---|---|---|
\' | ' | Single quote |
\" | " | Double quote |
\\ | \ | Backslash |
let x = "We are the so-called \"Vikings\" from the north.";4. Template Literals (ES6)
Template Literals use backticks (``) rather than quotes to define a string. With template literals, you can use both single and double quotes inside a string:
let text = `He's often called "Johnny"`;Template literals also allow multi-line strings:
let text =
`The quick
brown fox
jumps over
the lazy dog`;5. String Interpolation
Template literals allow variables and expressions in strings:
let firstName = "John";
let lastName = "Doe";
let text = `Welcome <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>f</mi><mi>i</mi><mi>r</mi><mi>s</mi><mi>t</mi><mi>N</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">{firstName}, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal">i</span><span class="mord mathnormal">rs</span><span class="mord mathnormal" style="margin-right:0.10903em;">tN</span><span class="mord mathnormal">am</span><span class="mord mathnormal">e</span></span><span class="mpunct">,</span></span></span></span>{lastName}!`;String interpolation is the preferred way to join (concatenate) strings in modern JavaScript development.
Top Interview Questions
?Interview Question
Q:What is the difference between single, double, and backtick quotes?
A:
Single and double quotes are mostly identical for defining simple strings. Backticks (template literals) allow for multi-line strings, string interpolation using ${}, and including both single and double quotes without needing escape characters.
?Interview Question
Q:How do you find the number of characters in a string?
A:
You use the .length property on any string variable (e.g., myString.length).
?Interview Question
Q:What is an escape character?
A:
An escape character () is used to tell JavaScript that a special character (like a quote or backslash) should be treated as literal text instead of a part of the language syntax.
Course4All Editorial Board
Verified ExpertSenior Full-Stack Engineers & V8 Experts
Our JavaScript and engine-level content is developed by a collective of senior engineers focused on high-performance web architecture and 2026 standards.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.