A string is text — a sequence of characters wrapped in quotes. Strings are one of the most-used data types, since almost every program deals with text somewhere: names, messages, file paths, URLs.
let greeting = "Hello, world!";
let single = 'Single quotes work too';let first = "Ada";
let last = "Lovelace";
let fullName = first + " " + last;
console.log(fullName); // "Ada Lovelace"| Method | What it does | Example |
|---|---|---|
.length | Number of characters | "hello".length → 5 |
.toUpperCase() | Converts to uppercase | "hi".toUpperCase() → "HI" |
.includes(x) | Checks if text contains x | "hello".includes("ell") → true |
.slice(a, b) | Extracts part of a string | "hello".slice(0, 3) → "hel" |
Backticks let you embed variables directly inside a string, instead of joining pieces with +.
let name = "Ada";
let age = 28;
console.log(`${name} is ${age} years old.`);
// "Ada is 28 years old."