Strings

Text manipulation. Immutable sequences of characters.

String Basics

Three ways to create. All are immutable.

const a = "double quotes";
const b = 'single quotes';
const c = `template literal`;
H0
e1
l2
l3
o4
,5
6
W7
o8
r9
l10
d11
!12

Each character has a zero-based index. Strings are like read-only arrays of characters.

Template Literals

Interpolation, multi-line, and tagged templates.

const name = "World";
const greeting = `Hello, ${name}!`;
// "Hello, World!"

Backtick strings allow embedded expressions with ${...}

1 / 3

String Methods

Transform, search, and extract.

.toUpperCase()HELLO, WORLD!
.toLowerCase()hello, world!
.trim()Hello, World!
.split(', ')["Hello","World!"]
.includes('World')true
.replace('World', 'JS')Hello, JS!
.slice(0, 5)Hello
.length13

Immutability

Strings never change. Methods return new strings.

let str = "Hello";
str[0] = "J";      // ✗ silently fails
str.toUpperCase(); // returns "HELLO"
console.log(str);  // still "Hello"

// To change, reassign:
str = "J" + str.slice(1); // "Jello"

Every string method creates a new string. The original is never modified.