Course contentsShow
← JavaScript
Lesson 182 of 2,7235. StringsFree lesson

Immutability: All Methods Return New Strings

Understand that string transformation methods never modify the original, always returning new string values.

Immutability: All Methods Return New Strings

You'll learn: Why string methods in JavaScript never change the original string, always creating and returning new ones instead.

The Core Concept

In JavaScript, strings are immutable, which means once a string is created, it can never be modified. When you call methods like toLowerCase(), trim(), replace(), or slice(), the original string remains completely unchanged. Instead, these methods return a brand new string with the transformation applied.

Think of strings like words carved into stone tablets. You can't edit the stone itself—you can only create a new tablet with different text on it.

How It Works in Practice

When you use any string transformation method, you must capture the returned value if you want to use it:

let original = "Hello World";
original.toLowerCase();  // Returns "hello world" but original is unchanged
console.log(original);   // Still "Hello World"

let transformed = original.toLowerCase();
console.log(transformed); // "hello world"
console.log(original);    // Still "Hello World"

This applies to every string method you've learned: trim(), replace(), slice(), repeat(), padStart(), and all the rest.

Why This Matters

This immutability is why chaining works so well—each method returns a fresh string that the next method can work on, without any risk of accidentally corrupting data elsewhere in your program.

Key Takeaway: String methods never mutate the original string; they always return new strings. Always capture the return value if you need the transformed result.