You are currently viewing Difference Between let and var and const in JavaScript.

Difference Between let and var and const in JavaScript.

What is the difference between let and var and const in javascript?

In this article, we will see what is the difference between let and var and const in javascript.

Before ES6, in javascript to define any variable, the var keyword was used. But now var keyword is not used at all. Because due to some reasons bugs can be introduced in your code. So, var is found in ES6 and as a rule of thumb, we should avoid it. Const is used to declare constants. Constants are those identifiers whose value cannot be changed throughout the javascript program. We use the let keyword, to declare block scoped variable.

Here I wrote the Example code

var a =45;
var b = "John";
var c = null
var d = undefined

Here string, integer, null and undefined values ​​are all stored using the var keyword So, in javascript firstly it was like this, to create variable use the var keyword. That’s it, that was the end of the story. But it is not like this today’s date. As javascript evolved and was used to make big web applications at that time people realized, This javascript. That we can’t change the constant. If a variable is used in a function, globally no one should change the scope variable and so on.

So let of const was introduced, it was said that the usage of var should is reduced. So, var is before ES6, which means ECMA script 6 and it is modern javascript. var is globally scoped, which means if you made a variable, then you can change it from anywhere. But let and const are blocked scope, which means we can’t change the value.

Leave a Reply