DEV Community

Sojin Samuel
Sojin Samuel

Posted on

1 1

Why String is immutable in Javascript (Help)

Top comments (1)

Collapse
 
peerreynders profile image
peerreynders • Edited

Because a string is a primitive value and in JavaScript all primitive values are immutable.

ECMAScript Specification: 4.4.20 String value:
"primitive value that is a finite ordered sequence of zero or more 16-bit unsigned integer values".

So I guess the next question is "why is a string a primitive value"?

Java released shortly before JavaScript and Java's strings where immutable for security reasons, so I wouldn't be surprised if that influenced the choice. I would also imagine that immutable strings are easier to handle by the runtime in a more performant manner.

Just yesterday I wrote this comment - it touches on the impact of primitive values being immutable.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay