0

In javascript only object and arrays are mutable and remaining are primitive type which are immutable. However if we create an object of string,then it can't be mutated. why is that?

var a = new String("abc")
a.toUpperCase();
console.log(a) /*wont print "ABC" and prints an object with String:abc. Concludes 
that string object is immutable.*/
Abhishek
  • 546
  • 5
  • 13
  • 2
    "*Concludes that string object is immutable.*" No, it concludes that *this method* does not mutate the instance. If you were to do `a.foo = "hello"` and then `console.log(a.foo)` you'd see that the instance *has* been mutated. – VLAZ Nov 01 '21 at 06:47
  • [More examples of your title](https://www.google.com/search?q=javascript+string+object+mutable+site%3Astackoverflow.com) – mplungjan Nov 01 '21 at 06:47
  • @VLAZ its a.foo which returns a mutated value. i was talking about a. – Abhishek Nov 01 '21 at 06:52
  • What is your source for the split in mutability between object/array and "primitive" types? – msbit Nov 01 '21 at 06:53
  • Many dupes like [this](https://stackoverflow.com/questions/35443406/how-to-set-update-the-value-of-a-string-object-in-javascript) and [this](https://stackoverflow.com/questions/51185/are-javascript-strings-immutable-do-i-need-a-string-builder-in-javascript) – mplungjan Nov 01 '21 at 07:04
  • @abhishek61067 `a` ***is*** the string object. It is mutable as I showed above, since you can change it. Mutation = change. A *method* is not obliged to mutate the associated object. – VLAZ Nov 01 '21 at 07:04

1 Answers1

3

In general, yes, objects are mutable.

The reason that "string objects" - created with new String - are not mutable is that string objects result in objects which have an internal StringData property:

Set S.[[StringData]] to value.

That StringData is what is retrieved when the string object is coerced back to a primitive.

If the interpreter provided a way to change this StringData internal slot - then these sorts of objects would effectively be mutable. (Similarly, Set.add changes a Set's internal slot, and the same sort of mechanism exists for many other objects in JS - an exposed method changes values held in internal slots.)

But

a.toUpperCase();

alone wouldn't do anything - toUpperCase returns a new string. There are no methods (in standard implementations) that exist that can alter a string object's StringData internal slot - all you can do is create a new string object with a different StringData.

You could also strongly consider just not using string objects at all.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • So that mean, using string literal and string object are pretty similar? Both are immutables! – Abhishek Nov 01 '21 at 06:56
  • 1
    Kind of. The string object, like most objects, is mutable, it's just that you can't alter *one particular property on it* (that internal slot). – CertainPerformance Nov 01 '21 at 06:58
  • Thanks for your helpful info! @CertainPerformance – Abhishek Nov 01 '21 at 07:02
  • So you reverted my dupe close so you could answer with a dupe? https://stackoverflow.com/questions/35443406/how-to-set-update-the-value-of-a-string-object-in-javascript https://stackoverflow.com/questions/51185/are-javascript-strings-immutable-do-i-need-a-string-builder-in-javascript – mplungjan Nov 01 '21 at 07:04
  • I disagree that string objects are not mutable. They *are*. The data they hold isn't. This seems like a very important difference since OP does not seem to make it. – VLAZ Nov 01 '21 at 07:13