DEV Community

You either 'let', 'var' or keep 'const' in JavaScript

Lody G Mtui on June 12, 2022

Difference between var, let and const In JavaScript, variables are assigned using three main keywords/ways which are:- var let const....
Collapse
 
moopet profile image
Ben Sinclair

And const can neither be re-declared nor updated.

Const can be updated if it's an object or array. This is fine, for example:

const foo = [];

foo.push("hello");
Enter fullscreen mode Exit fullscreen mode

So const is kinda-sorta-const, depending where you learned the word "constant". You can't redeclare it or make it point at anything else, but you can modify it if it's a container for other things.

Collapse
 
jzombie profile image
jzombie

Const gives you a constant memory reference. You can't reassign the memory reference, but any properties can be [re]assigned. The constant value assumption is only for primitive types.

To freeze objects: developer.mozilla.org/en-US/docs/W...

Collapse
 
mistval profile image
Randall • Edited

It's not a reference to specific memory. The thing pointed to by a const variable can and often will move locations in memory (for example if you keep pushing onto an array, the engine will eventually have to allocate a new, bigger block of memory for it). It's just a constant reference to a specific value.

Thread Thread
 
jzombie profile image
jzombie

Thanks for the clarification.

Collapse
 
lodyne profile image
Lody G Mtui

I'll check on this. Thanks for the feedback

Collapse
 
lodyne profile image
Lody G Mtui

Thanks for this information, I will learn it and update it

Collapse
 
steveknoblock profile image
Steve Knoblock

The computer science definition of constant existed for forty years or more before JavaScript decided to use the same term for something different than most computer languages and in mathematics.