Are javascript stri...
 
Share:
Notifications
Clear all

Are javascript strings immutable?

Alex Disney
(@alexdisney)
Eminent Member

Are javascript strings immutable?

Quote
Topic starter Posted : 09/05/2022 5:15 pm
a70c51106900eba242692ba52c12d253?s=110&d=mm&r=g
(@vishaljaiswal)
Member Admin

Are JavaScript strings immutable? This is a question that often comes up in programming circles. The answer, unfortunately, is not a simple yes or no.

There are two ways to think about this question. The first way is to think about whether or not strings can be changed after they are created. The second way is to think about whether or not strings can be changed in place.

In terms of the first question, the answer is yes, strings can be changed after they are created. This is because strings are represented as objects in JavaScript. As such, they can be modified using the standard object methods.

In terms of the second question, the answer is a bit more complicated. While it is possible to change the characters in a string, it is not possible to change the length of a string. This is because the length of a string is fixed at the time of creation.

Overall, the answer to the question of whether or not JavaScript strings are immutable is a bit complicated. It depends on how you think about the question.

ReplyQuote
Posted : 09/05/2022 5:15 pm
Share: