String in JavaScript: working with text you cannot modify

A String represents text in JavaScript. It is immutable: every method returns a new string instead of changing the original one.
3 min read
Believemy logo

Everything that comes from outside a program arrives as text. A form field, a URL parameter, a raw API response: the string is the default input format, and knowing how to slice one cleanly saves a great deal of time.

One property sets it apart from most other structures: it never changes. Every method returns a new string, which has immediate practical consequences.


Definition

A String is a sequence of characters, written between single quotes, double quotes or backticks. All three produce the same type, and only backticks allow variables to be interpolated.

JAVASCRIPT
const title = "  JavaScript for the web  ";

console.log(title.length);            // 26, spaces included
console.log(title.trim());            // "JavaScript for the web"
console.log(title.includes("web"));   // true
console.log(title.trim().toUpperCase());

const left = 3;
console.log(`There are ${left} seats left`);  // "There are 3 seats left"

Methods chain because each one returns a string, which lets you read a transformation from left to right in the order it happens.


The methods you reach for daily

MethodWhat it does
trim()Removes spaces at both ends
split(separator)Cuts into an Array
replace(a, b)Replaces the first occurrence
slice(start, end)Extracts a portion
includes(text)Answers yes or no
padStart(n, char)Pads on the left, handy for clock times
JAVASCRIPT
console.log("a,b,c".split(","));        // ["a", "b", "c"]
console.log("Hello".slice(0, 3));       // "Hel"
console.log("price: 49".replace("49", "59"));  // "price: 59"
console.log("7".padStart(2, "0"));      // "07"
Good to know

replace only replaces the first occurrence when given plain text. To replace them all, use replaceAll, or a regular expression with RegExp.


Immutability, and what it implies

A string never changes in place. Writing to a character by its index does nothing in sloppy mode, and throws in strict mode, which is what modules run in.

JAVASCRIPT
const word = "abc";
const other = word.replace("a", "z");

console.log(other);  // "zbc"
console.log(word);   // "abc", unchanged

The practical consequence is simple: the result of a method must always be captured. Forgetting the assignment is the most common string mistake, and it produces no message at all.


Frequently asked questions

Question

Single quotes, double quotes or backticks?

Single and double quotes are strictly equivalent, the choice is a team convention. Backticks add two real capabilities: dropping a variable straight into the text, and writing a string across several lines. They are now the most used form whenever a variable has to be inserted.


Question

Why does "5" + 3 give "53"?

Because the + operator concatenates as soon as one of the two values is a string, instead of adding. The number is turned into text automatically. This behavior belongs to Coercion (type conversion), and the fix is to convert explicitly with Number before any calculation.


Question

Why do some characters count as two?

Because length counts code units, not visible characters. Emoji and some symbols take two units, so "👍".length is 2. To count the characters actually displayed, the trick is to convert to an array with Spread (...), which splits correctly.

Related terms

Discover our javaScript glossary

Every word of JavaScript explained simply: keywords, built-in objects, methods, errors and concepts. Clear definitions and examples that actually run, to learn and to troubleshoot.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.