DEV Community

Discussion on: Not everything is an object in JavaScript

Collapse
 
mmartinezsoria profile image
Maximo Martinez Soria

I understand your point. But null is a primitive value. This is supported by the official documentation: developer.mozilla.org/en-US/docs/W...

Collapse
 
pentacular profile image
pentacular

Being primitive isn't significant.

null is the value used to express that something has been set to refer to no existing object (as opposed to undefined which expresses that something has not been set).

As such, being of type object makes sense, since it is to be used where objects are used.

JavaScript use two kind of memories: Memory Heap and Memory Stack.

Memory Stack, is where primitives are saved. This memory is smaller, but it's faster than the Memory Heap. On the other hand, Memory Heap is bigger, but smaller.

This is incorrect.

Javascript does not divide memory like this, although some implementations may.

An object value is exactly like a primitive value -- it is immutable.

What an object value does is to allow you to access properties, and it is those properties that are mutable -- the properties are not part of the object value, otherwise changing a property would make a === b no-longer true.

As to where and how the properties are stored, that is an implementation decision.

Thread Thread
 
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.

Collapse
 
pentacular profile image
pentacular
  1. Being a primitive value has nothing to do with being of type 'object'. Belonging to a type is about how things are categorized, and is essentially arbitrary.
  2. Mozilla's documentation is not the standard for Javascript (ecmascript) - it talks about mozilla's implementation.

To understand javascript you need to look at the ecmascript standard.

ecma-international.org/publication...