DEV Community

Discussion on: JavaScript-30-Day-14

Collapse
 
cenacr007_harsh profile image
KUMAR HARSH

Thanks for pointing this out and the detailed explanation, the instructor never mentioned it maybe he wanted to keep things simple and that is how it works in other languages like C++ so it never occurred to me to get into depth.

Collapse
 
peerreynders profile image
peerreynders • Edited

that is how it works in other languages like C++

In C++ :

  int n = 5;    // this declares a variable, n 
  int & r = n;  // this declares r as a reference to n 
Enter fullscreen mode Exit fullscreen mode

i.e. n is bound to one specific memory location but the value inside that location can be changed. r is an alias that refers to exactly the same memory location.

Now from const:

The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned.

This is where it leaks through that JavaScript doesn't have "variables" in the sense of

  • the name that refers to one and only one memory location which contains some value that can change

but instead implements

  • the name that refers to one and only one memory location which contains a reference to the value which the name is bound to at this point in time

maybe he wanted to keep things simple.

Many learning materials do this because they begin with primitive values and it is felt that the extra indirection introduced by references is just going to confuse the beginning student. Which makes sense but this shapes the incorrect mental model.
By the time arrays and objects are introduced the notion of a reference needs to be understood anyway - and it is at this point that the mental model regarding variables and primitive values needs to be adjusted - which rarely happens and which leads to confusion later.

As const demonstrates - variable( reference)s behave exactly the same for primitive values and objects because assignment doesn't change the value but changes which value is bound to the name.

Thread Thread
 
cenacr007_harsh profile image
KUMAR HARSH

"This is where it leaks through that JavaScript doesn't have "variables" in the sense of

the name that refers to one and only one memory location which contains some value that can change
but instead implements

the name that refers to one and only one memory location which contains a reference to the value which the name is bound to at this point in time"

I would like to understand this in more detail, about how variables work, can you point me to a source where I can learn about this in greater depth.

Thread Thread
 
peerreynders profile image
peerreynders

Look, there are lots of sources that will say that I'm wrong (and perhaps I am) - for example:

When assigning a primitive value to a variable or passing it as an argument to a function, its content is copied.

While this superficially works as a mental model (especially if you are trying to avoid talking about references) it makes no sense if all primitive values are immutable - duplicating immutable values is simply wasteful.

From What Does it Mean that Primitive Values are Immutable in JavaScript?:

Variables just point to values.

The "content is copied" mindset fosters the view that "variables point to the same memory address for their entire lifetime" - for example:

let x = 123;

// Content for variable `x` will always 
// live at address `#a` so:
// #a: 123

let y = x;
// Content for variable `y` will always 
// live at address `#b` 
// and content is copied, so:
// #a: 123
// #b: 123

y += 1;
// We only change the content for `y`, so:
// #a: 123
// #b: 124
//
// Note that the value at `#b` changed. 
// This violates the assertion 
// that primitive values are immutable.
Enter fullscreen mode Exit fullscreen mode

Compare that to the "variables just point to values" mindset:

let x = 123;

// Create a new immutable value at memory location `#a` 
// and have `x` point to it: 
// #a: 123
// x: #a

let y = x;
// `y` needs to point to the same value as `x` 
// so just _copy the reference_:
// #a: 123
// x: #a
// y: #a

y += 1;
// Primitive values are immutable 
// so we create the new value `124` 
// and store it at memory location `#b` 
// and have `y` refer to it: 
// #a: 123
// #b: 124
// x: #a
// y: #b
Enter fullscreen mode Exit fullscreen mode

The thing is the "variables just point to values" view simplifies everything because now variables work exactly the same for primitive and non-primitive values and it also explains the behaviour of const - const makes the reference used by the variable immutable, so you're stuck with the same primitive or non-primitive value for the lifetime of that variable; however that doesn't affect the internal mutability of non-primitive values.

Thread Thread
 
cenacr007_harsh profile image
KUMAR HARSH

That was some great explanation, thanks for taking out the time to explain it so clearly.