DEV Community

Cover image for When should we use Let and const in our code🤔.
Amol Shelke
Amol Shelke

Posted on

When should we use Let and const in our code🤔.

Why we should use let and const in our code?

So the Let and Const come in ES6 Update and the let and const are often used by many developers, if you went and watch a tutorial now, you'll get to see let and const everywhere.
So the let is use to declare a variable, and we can reassign the value of the let declare variable anywhere in our program.
And the Const is used to declare a variable, and we cannot mute the value of the const to declare the variable in our program.

So when we use let? and when we use const?

use let when you are sure that you will need to change the value of a variable throughout your program.

For Example

let age = 19;
Enter fullscreen mode Exit fullscreen mode

So in this example, I declare a variable age with the let keyword, because I know that the value of the age variable is need to be changed after a year. and I can change it in my code anywhere. like this.

let age = 20;
Enter fullscreen mode Exit fullscreen mode

and use const when you are sure that you will not need to change the value of a variable throughout your program.

For Example

const birthYear = 20;
Enter fullscreen mode Exit fullscreen mode

So in this example, I declare a variable with the const keyword, because I know that the value of the birthYear variable is don't need to change anywhere in my program. And same thing with any variable that are declare with const variable we can't reassigned the value of them or cannot change them in anywhere in our program.

Top comments (0)