DEV Community

Nuno Pereira
Nuno Pereira

Posted on • Edited on

6

Differences between "let" and "const" keywords?

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 !

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (2)

Collapse
 
jaloplo profile image
Jaime López

Hi Nuno,

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

Collapse
 
nunocpnp profile image
Nuno Pereira • 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.

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️