Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 17, 2022 05:50 am GMT

Are JavaScript strings immutable?

String specifications among programming languages vary, however most languages treat them as reference types. But strings in JavaScript are different. They are immutable primitives. This means that the characters within them may not be changed and that any operations on strings actually create new strings.

const x = 'type';x[1] = 'a';       // Nothing happens, doesn't throw an errorconsole.log(x);   // LOGS: 'type'

It helps to think of strings in the way that we think of numbers, if we are to better understand how they work. Numeric values are also immutable primitives. If, for example, you could mutate numbers, you could change the meaning of the value 42 to be another number, say 13. In the same sense, you cannot mutate a string with a value of gem to make it gym.

Do you like short, high-quality code snippets and articles? So do we! Visit 30 seconds of code for more articles like this one or follow us on Twitter for daily JavaScript, React and Python snippets!


Original Link: https://dev.to/chalarangelo/are-javascript-strings-immutable-13b3

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To