You will be able to create JavaScript strings as text values using quotes, backticks, and conversion, and explain when each form is appropriate.
What it is
A JavaScript string is a primitive text value: an ordered sequence of characters such as letters, digits, spaces, and punctuation. Strings are immutable, meaning once a string exists, its characters cannot be changed in place. Operations like toUpperCase() return a new string instead of modifying the original.
Related terms include string literal, template literal, interpolation, escape sequence, and string conversion. A literal is text written directly in code. A template literal is a special string form that can embed expressions.
Why it matters
- Strings store user names, messages, URLs, file paths, and form input.
- Template literals make dynamic text easier to read than many
+concatenations. - String conversion lets numbers, booleans, and other values become text for display.
- Understanding immutability prevents bugs when editing text.
- Quotes and escaping are needed when text contains punctuation or code.
Syntax or steps
The smallest useful pattern is a string literal assigned to a variable:
const s = "Hello";
JavaScript accepts three common literal forms:
const single = 'Hello';
const double = "Hello";
const template = `Hello`;
Use String(value) to convert values such as numbers and booleans into a string:
const year = String(2026);
Inside a template literal, ${expression} inserts the result of an expression:
const name = "Ada";
const greeting = `Hello, ${name}!`;
Example
const city = "Paris";
const population = 2100000;
const label = `City: ${city}`;
const combined = "Population: " + population;
const converted = String(true);
console.log(label);
console.log(combined);
console.log(converted.length);
Part by part:
const city = "Paris";creates a double-quoted string.`City: ${city}`creates a template literal and inserts the value ofcity."Population: " + populationconcatenates a string with a number, producing a new string.String(true)converts the booleantrueinto the string"true".converted.lengthreads the number of UTF-16 code units in the string.
Common mistakes
- Unescaped quotes.
'It's done'is invalid because the second quote ends the string. Fix it with"It's done"or'It\'s done'. - Using normal quotes for interpolation.
'Hello, ${name}'prints the literal text${name}. Fix it by using backticks. - Trying to mutate a string.
s[0] = "h"does not changes. Fix it by creating a new string, such ass.replace("H", "h"). - Confusing concatenation with addition.
"5" + 1produces"51", not6. Fix it by converting first, such asNumber("5") + 1.
When to use it
Choose the form based on whether you need simple text, dynamic text, or conversion.
| Form | Use when | Example |
|---|---|---|
| Single or double quotes | You need a simple literal string. | const s = "Hello"; |
| Template literal | You need interpolation or multiline text. | const s = `Hello, ${name}!`; |
String(value) | You need to convert a non-string value to text. | const s = String(42); |
Prefer template literals over long concatenation chains when readability matters. Avoid new String("text") unless you specifically need an object wrapper; it is rarely useful in normal code.
Practice
- Guided exercise: Create a variable named
languagewith the value"JavaScript". Then createmessageusing a template literal that saysI am learning JavaScript.. Expected output:I am learning JavaScript. - Challenge: Convert the number
100to a string and concatenate it with" points". Hint: useString(100) + " points"or`${100} points`.
Quick check
Question: Which string form allows ${expression} interpolation?
Answer: A template literal written with backticks.
Summary
JavaScript strings are immutable text values created with quotes, backticks, or conversion functions. Use simple literals for fixed text, template literals for dynamic text, and String() when converting other values into text.