DEV Community

loading...

Differences between "let" and "const" keywords?

nunocpnp profile image Nuno Pereira Updated on ・2 min read

In my last article I talked about the differences between "var" and "let" now it's time to talk about the differences between "let" and "const" keywords.

Let's gets started !

“let” and “const” they are both used to define variables.

Difference nr 1!

With keyword “const” you can define constant variables which means, after the first assignment of the value you cannot reassign the value again.

With keyword “let” you can reassign the value as many times as you need, you can even change the type of value if needed.

Let’s take a look at some code:

let l = 1;
l = 2;
console.log(l); 
// 2

const c = 1;
c = 2;
Console.log(c); 
// Error: Uncaught TypeError: Assignment to constant variable 

Difference nr 2!

Variables defined with “const” keyword cannot be initialized without any value. You must specify its value in the same statement in which its declared (which makes sense, given that it can't be changed later).

Let’s take a look at this example:

let l
l = 1
console.log(l)
// 1

const c;
c = 1;
console.log(c);
// Error: Uncaught TypeError: Missing initializer on constant variable

Difference nr 3!

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. For instance, in the case where the content is an object, this means the object's contents can be altered.

Let’s see this in action:

const c = [ 1, 2 ];
c.push(3);
console.log(c);
// [ 1, 2, 3 ]

Const c = [ 1, 2 ];
C = [ 1, 2, 3 ];
console.log(c);
// Error: Uncaught TypeError: Assignment to constant variable 

That's it ! Once again simple but important concepts to keep fresh in your minds.

See you soon for more tips !

Discussion (3)

pic
Editor guide
Collapse
jaloplo profile image
Jaime López

Hi Nuno,

Is it possible to assign null value to a const variable? And undefined?

Collapse
yujiri8 profile image
Ryan Westlund

You know, you could've just opened a JS console or Node and tried it :P

Collapse
nunocpnp profile image
Nuno Pereira Author • Edited

null is a primitive values, so in theory you can but I believe there is no point in it because primitive values are immutable so there will be no way to mutate this value.