JavaScript chapters

Chapter 11 of 17

Strings

Try this in our JavaScript Compiler →

Text is everywhere

Almost every program reads, manipulates and prints text. JavaScript represents text as a string — a sequence of characters. Strings are immutable in many languages, meaning every "change" actually produces a brand new string and leaves the original alone.

const name = "PlayLearn";
console.log(name.length);
console.log(name.toUpperCase());
console.log(name.includes("Learn"));
const greet = `Hello, ${name}!`;

Common string operations

Concatenation joins two strings together. Slicing extracts a portion. Searching finds whether a substring exists. Splitting breaks a string into pieces around a separator. Replacing swaps one substring for another. These five operations cover the vast majority of real-world text work.

Formatting and templates

When you need to drop variables into a string, prefer the language's built-in template syntax over manual concatenation. It is shorter, harder to get wrong, and produces clearer code that other developers can scan in a glance.

Try it yourself

JavaScript Compiler
Output
Code runs in a sandboxed preview inside your browser — your code is saved locally for next time.