How to work with strings

Create strings

Single and double quotes both create strings. Escape a matching quote or choose the other delimiter.

const first = "JavaScript";
const sentence = 'She said "hello".';
const path = "C:\\Users\\Learner";
console.log(first, sentence, path);

Build text with template literals

Backticks support interpolation with ${...} and can span lines.

const name = "Sam";
const completed = 4;
console.log(`${name} completed ${completed + 1} lessons.`);

Inspect and transform text

Strings are immutable: methods return new strings rather than changing the original.

const raw = "  Learn JavaScript  ";
const clean = raw.trim();
console.log(clean.length);
console.log(clean.toUpperCase());
console.log(clean.includes("Java"));

Extract and split text

Indexes start at zero. Use slice for a portion and split to make an array.

const language = "JavaScript";
console.log(language[0]);       // J
console.log(language.slice(4)); // Script
console.log("red,green,blue".split(","));