Difference between const vs let

In this article you learn what is difference between const vs let. You will learn what are they and how to used them.

var creates a variable in JavaScript. But some values are never changed of variables. They are constants. But with ES6 (a version of JavaScript) two new JavaScript keywords were introduced: let & const. let & const are different ways to create variables. var still works but its good to use let and const.

const

Use const if variable value is constant and it will not be changed. const variable values cannot be changed.

Example:

const name = "John";
console.log(name);

name = "Jane";
console.log(name);

Output:

You will get the following error as you cannot assign another value to const once it is assigned. So, use const only when you know that value is not going to change.

redeclaring-const-error

let

let is new var. We use it to store variable values. Use let to declare variables if that variable value will be changed.

Example:

let name = "John";
console.log(name);

name = "Jane";
console.log(name);

Output:

John
Jane

If you have any question about this topic, you can ask me through the comment section.

Leave a Reply

Your email address will not be published. Required fields are marked *