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
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
Thanks for reading 💙
Follow @codedrops.tech for daily posts.
Instagram ● Twitter ● Facebook
Micro-Learning ● Web Development ● Javascript ● MERN stack ● Javascript
codedrops.tech
Top comments (5)
Actually
MDN: const
MDN: Primitive
So primitives already cannot change.
let
creates a binding that can be changed while aconst
binding cannot change.However structural types like
object
(which includes arrays) andfunction
are mutable. So the readonly reference ofconst
to structural types doesn't influence their mutability. Internally the instance of a structural type remains mutable. However theconst
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.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 :)
reinforces a faulty mental model
i.e. that
value
is a place and the456
replaces123
in that place - when in factvalue
is simply a name that refers to the place where123
is and that name can later refer to a different place where456
is. So withvalue
can only ever refer to one and the same place - the one where123
is.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.
Wow! That was nice.