Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

String immutability

JavaScript strings are immutable: once created, their contents cannot be changed. String operations return new strings, so retain the result when you need transformed text.

How it works

A JavaScript string cannot be changed after it is created. Operations that appear to edit text instead produce another string, which you can store by assigning the returned value to a variable; the variable itself may still be reassigned.

Syntax

const result = string.method(...args);

Returns

String operations that transform or combine text return a new string; the original remains unchanged.

Key points

  • String immutability is part of the broader rule that primitive values cannot be altered in place.
  • Existing string contents remain fixed for their entire lifetime.
  • Methods that transform or extract text produce a separate string value.
  • Combining strings also creates a result rather than extending either operand.
  • The returned value is the place to find the transformed text.

Common mistakes

  • Character assignment does not modify a string; use an operation that creates a replacement string and save its result.

Example: Create a changed string

const original = "hello";
const changed = original.replace("h", "H");

console.log(original); // "hello"
console.log(changed);  // "Hello"

replace() returns a new string, so the original value remains unchanged.

When to use it

Text transformation

Normalize or transform user input without altering the original value.

String composition

Build labels, messages, or paths from existing string values.

Non-destructive updates

Preserve an original value while producing an edited variant.

Interview questions

QWhat does string immutability mean in JavaScript?

Strings are immutable primitive values, so their contents cannot be modified in place. Methods such as replace() return a new string rather than changing the receiver.

Follow-up: What happens if a variable holds a string? The variable can be reassigned to the newly returned string, but the original string value is still unchanged.

QHow can you change one character in a string?

An assignment such as str[0] = "H" cannot replace the first character of a string. To change the text, construct a new string and assign that new value to the variable.

Follow-up: How would you replace every occurrence? Use replaceAll(), or use replace() with a global regular expression.

QWhy must the result of a string method be assigned?

String transformation methods return new strings because primitive values are immutable. Therefore, code must capture the return value when it needs the transformed text.

Follow-up: Does const make the string immutable? No: the string value is immutable independently; const only prevents rebinding that particular variable.

  • String
  • String.prototype.replace()
  • String.prototype.replaceAll()
  • String.prototype.concat()
  • String.prototype.substring()
  • Primitive

Source: JavaScript data types and data structures. Checked 2026-10-15.