DEV Community

Discussion on: Not everything is an object in JavaScript

 
mmartinezsoria profile image
Maximo Martinez Soria

All I wrote is supported by the docs.

Mutability:
developer.mozilla.org/en-US/docs/G...

Memory:
developer.mozilla.org/en-US/docs/W...
developer.mozilla.org/en-US/docs/W...

And I quote:

  • “Objects are allocated in a heap”
  • “Mutable is a type of variable that can be changed. In JavaScript, only objects and arrays are mutable, not primitive values.”
Thread Thread
 
pentacular profile image
pentacular

You're reading some documentation for Mozilla, not Javascript.

Mutable is a type of variable that can be changed. In JavaScript, only objects and arrays are mutable, not primitive values.

This is obviously wrong, since objects and arrays are not variables.

Next you need to understand what mutability for objects and arrays means.

let a = { name: 'a' };
let b = { name: 'b' };
let c = a;

// This changes a property of a, but does not change the value of a.
// c === a remains true.
a.name = 'c';

// This changes the value of a, but does not change the properties of that value.
// c === a becomes false
a = b;

What this means is that the value of the object is immutable, but properties of an object may be mutable.

If you read the ecmacscript standard, you'll find exactly zero mention of 'heap' -- because this is an implementation detail, not part of the language.

ecma-international.org/publication...

Thread Thread
 
mmartinezsoria profile image
Maximo Martinez Soria • Edited

I don't think that example is correct.
Just give it a try.

After a.name = 'c', c === a is still true.
Changes are applied to both variables, because they are just a reference.

On the other hand, I agree about ecmascript. But Mozilla is just a common implementation.

And btw, thank you for taking your time to contribute to the discussion. I find really interesting to know different points of view.

Thread Thread
 
pentacular profile image
pentacular

a.name = 'c'; does not change any variable.

That's why the variables are still the same.

There are no references involved.