Responsive Advertisement

The difference between let, var, and const in JavaScript

The difference between let, var, and const in JavaScript

The difference between let, var, and const in JavaScript

The three different ways of declaring variables in JavaScript are let, var, and const. let and const were introduced in the ES6 version of JavaScript, and so they are modern JavaScript. Declaring variables with the var keyword is the old technique. Let's look at the differences between them and when to use which one.

So anyway, we use the let keyword to declare variables that can change the variable value later, so basically during the execution of our program, and that's exactly what we did in line number 2 in the example code snippet. The age variable (which we declare in line number 1) is set to 30 at one point in the program. And then at another point in the program, we can change the variable value to something else. For example, when the person turns 26, the age, of course, changes from 26 to 27. As a result, defining a variable with let and then assigning a new value to it later in the program is perfectly legal. In other words, we call this reassigning a value to a variable, or we also say that we mutate the age variable in this case. So, it was 26 and now it's 27. We mutated the variable, okay? So, in the JavaScript programming world, that's a term you'll hear all the time. As a result, when we need to change a variable value, let is the appropriate method. And it's also perfect for the case that we want to declare empty or undefined variables. It may be a common question in your mind: why do we need to assign an empty or undefined value? For example, when we want to declare all the variables at the top of a file, but only assign actual values to them later in the program based on some condition.

The difference between let, var, and const in JavaScript Example

On the other hand, we use the const keyword to declare variables that are not supposed to change their value at any point in the future. The value of a const variable cannot be modified. Assume the value of the birthYear variable (which we declare in line 3) is 1994. If we then tried to reassign it, let's say to 1995 or something, it should not work. It generates an immutable variable or a variable that cannot be reassigned. The birth year is a perfect example of that, because in fact, the birth year of a person can never change, while the age, of course, can change. Another example is the value of PI. We should know that the PI value can never be changed.

let age = 26; // Block Scope
age = 27;
const birthYear = 1994; // Block Scope
fullName = "Touhid Hasan Akash"
console.log(fullName)  // Output : Touhid Hasan Akash
var someOne = "Atiq Rahman"; // Function Scope

Now, the fact that variables created with const are immutable also means that we cannot declare empty const variables. When using const, we must have an initial value. "Should I use let or const to declare a new variable?" you'll probably think. As a best practice for developing clean code, I usually recommend using const by default and let only when absolutely certain that the variable will change in the future. For example, when you have a variable that's really never supposed to change, like this birthYear variable, you should always use const. You should also declare the age variable as const if you are certain that it will never change within your program. The reason for this is that it's best practice to have as few variable mutations or modifications as possible because modifying variables might occur problems or errors in your code. So again, by default, always use just const and when you're sure that the value of the variable needs to change at some point in your code, then you can use the let keyword.

Variables Re-Declaear Re-Assign Hoisting Block Scope Create Global Scope
var Yes Yes Yes No Yes
let No Yes No Yes No
const No No No Yes No

Now, there's also a third way in JavaScript of declaring variables, which is the var keyword, but this one should actually be completely avoided, okay? However, we should still understand how it works for legacy reasons, as it may appear in older codebases. Let's have a look at how it performs. So, var is the old method of defining variables. It works pretty much the same as the let keyword. Although var and let seem to be very similar on the surface, they are actually extremely different. Actually, there are also many differences between let const and var keywords. I will describe it in depth in the other post. Now I just briefly try to make some sense of it. The let is block-scoped and the VAR is function-scoped. Now you can ask what block scope or function scop is. I'll go over the scoping in more detail in another post. Now you just need to know about this to know that these exist in the JavaScript programming world.

Other ways of declaring a variable name in JavaScript Look at lines numbers 4 and 5. We can just use the only variable name like fullName and by logging in to the console on line 5. Even without declaring a variable, JavaScript will happily execute this script. Right? We didn't use let, const, or a var keyword in this case, but it still worked. However, because it does not create a variable in the present so-called scope, this is a horrible idea. Instead, JavaScript will generate a global object attribute. You should always properly declare variables, okay? You should never write a variable like this without previously declaring it.

Post a Comment

0 Comments