DEV Community

loading...

What changes can be made with `const`

318097 profile image Mehul Lakhanpal Originally published at codedrops.tech ・1 min read

const is used when the value does not change. ex,

const name = 'Codedrops';
name = 'Codedrops.tech'; // Error

const list = [];
list = [1]; // Error

const obj = {};
obj = { name: 'Codedrops' }; // Error
Enter fullscreen mode Exit fullscreen mode

But it can be used to update value in previously assigned arrays/objects

const list = [];
list.push(1); // Works
list[0] = 2; // Works

const obj = {};
obj['name'] = 'Codedrops'; // Works
Enter fullscreen mode Exit fullscreen mode

Thanks for reading 💙

Follow @codedrops.tech for daily posts.

InstagramTwitterFacebook

Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript

codedrops.tech

Discussion (5)

pic
Editor guide
Collapse
peerreynders profile image
peerreynders • Edited

const is used when the value does not change

Actually

MDN: const

The const declaration creates a read-only reference to a value.

MDN: Primitive

There are 6 primitive data types: string, number, bigint, boolean, undefined, and symbol...
All primitives are immutable

So primitives already cannot change. let creates a binding that can be changed while a const binding cannot change.

However structural types like object (which includes arrays) and function are mutable. So the readonly reference of const to structural types doesn't influence their mutability. Internally the instance of a structural type remains mutable. However the const reference cannot be changed to refer to another value.

So it's not that value does not change (because in the case of an instance of a structural type it can) but the reference to the value cannot change.

  • let creates a reference that can be reassigned.
  • const creates a reference that cannot be reassigned.
  • primitive values are immutable.
  • instances of structural types are mutable.
Collapse
318097 profile image
Mehul Lakhanpal Author

Definitely. But when a person starts out you can't explain in such depth. And there are complex blogs so someone has to explain the easy way too :)

Collapse
peerreynders profile image
peerreynders • Edited

const is used when the value does not change

reinforces a faulty mental model

let value = 123;
value = 456;
Enter fullscreen mode Exit fullscreen mode

i.e. that value is a place and the 456 replaces 123 in that place - when in fact value is simply a name that refers to the place where 123 is and that name can later refer to a different place where 456 is. So with

const value = 123;
Enter fullscreen mode Exit fullscreen mode

value can only ever refer to one and the same place - the one where 123 is.

Thread Thread
318097 profile image
Mehul Lakhanpal Author

See no turns to an expert at day 1. People have to then understand that is a reference and a value. Everything has a dependency. And just reading it once won't build a model. Everyone has to study, practice and learn regularly to get good at anything.

Collapse
odionjulius4 profile image
Odion Julius

Wow! That was nice.