JavaScript Strings

How JavaScript Handles Text

JavaScript Strings

Strings are text. Learn how to write them, escape special characters, and combine them.

4 min read Level 1/5 #strings#text#literals
What you'll learn
  • Write string literals with single, double, or backtick quotes
  • Use escape sequences for special characters
  • Understand that strings are immutable

A string is a piece of text — anything from a single letter to a whole book. You write strings inside quotes.

String literals script.js
const single = 'Single quotes';
const double = "Double quotes";
const backtick = `Backticks`;

console.log(single, double, backtick);
▶ Preview: console

All three forms produce the same kind of value. Pick one for your project and stick with it. Modern JavaScript convention: single quotes for plain strings, backticks when you need interpolation or multi-line (covered in the Template Literals lesson).

Escape Sequences

What if you want to use a quote inside a string? Either switch quote styles, or escape it with \.

Escaping quotes script.js
const a = "It's a \"quoted\" word.";
const b = 'She said "hello".';
const c = `Backticks let you use 'both' "kinds" of quotes.`;

console.log(a);
console.log(b);
console.log(c);
▶ Preview: console

Other common escape sequences:

SequenceMeaning
\nnewline
\ttab
\\a literal backslash
\"a literal "
\'a literal '
\u{1F600}Unicode code point (😀)
Multi-line with \\n script.js
console.log("Line 1\nLine 2\nLine 3");
▶ Preview: console

Strings Are Immutable

Once you create a string, you can’t change it. Methods like toUpperCase return a new string — they don’t modify the original.

Strings don't mutate script.js
const greeting = "hello";
const shouted = greeting.toUpperCase();

console.log(greeting); // "hello"  ← unchanged
console.log(shouted);  // "HELLO"
▶ Preview: console

Length

Every string knows how long it is.

String length script.js
const name = "JavaScript";
console.log(name.length); // 10
▶ Preview: console

Length counts UTF-16 code units, not visible characters. Most of the time this is what you want; emoji and other multi-byte characters can return surprising lengths.

Up Next

Now that you can write strings, let’s see what you can do with them.

JavaScript String Methods →