- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript String Immutability
JavaScript Strings
JavaScript String Immutability
Strings cannot be changed; every method returns a new one.
This is the single rule behind the most common string mistake.
Calling a method and ignoring the result does nothing at all.
Example
Example
javascript
let text = "hello";
text.toUpperCase();
console.log(text);The output is hello: the result was thrown away.
What Immutable Means
The characters of a string can never be modified.
Every method returns a brand new string.
To keep the result, assign it or use it straight away.
