Up until this point, I only used the keyword
let to declare a variable; however, there are also
To be precise
var is the old way of declaring variables. So let's learn how they are different and which one to use in which situations.
We use the keyword
let to declare variables that can change later; basically, these variables change their values during the execution of our programme.
As the variable's value
true to string
Yes it is, we should prefer to use the keyword
let to declare such a variable. So, it's perfectly okay to declare a variable
let at one point in the programme and then later assign a new value.
In technical terms, we call this reassigning value to a variable, or also we can say that we mutate the
So, when we need to mutate a variable, that's the perfect use case for using
let and that also counts for the possibility that we want to declare empty variables.
Sometimes, we want to declare all the variables at the top of a file but only assign actual values to the variable later in the programme, for example, based on some condition.
We just discussed that
let is used when we need a mutating variable. On the other hand, we use the keyword
const to declare variables that are not supposed to change at a point in the future during a single execution. So the value in a
const variable cannot be changed.
Now the fact that variables created with
const are immutable implicitly means that we cannot declare
In technical terms, we use the keyword
const to create
immutable variables. A variable that cannot be mutated. The birth year is a perfect example of that because the birth year can never change. While the age, of course, can change.
Also, remember in the above example, in place of
const birthYear = 1996, I can also achieve the same results by using
let birthYear = 1996. But using
immutable the variable is considered a good coding practice.
const or let?
Now with these two different ways of declaring variables, you will probably ask should I use
const to declare a new variable?
Well, as a best practice for writing clean code, I will always recommend using
const by default and use
let only when you are sure that the variable needs to change at some point in the future. This is to have as few variable mutations as possible; because mutable variables introduce a potential to create bugs.
So, by default always prefer to use const.
var. But this one should be avoided entirely. However, we should still know how it works for legacy reasons. Maybe you will come across this on an older code basis or some older coding tutorials.
var is basically an old way of defining variables prior to ES6. And at first sight, it works pretty much the same as the
Although it looks like
let are similar. Below the surface, they're actually pretty different. And also, there are many differences between
var; you will learn all about these differences later in the DOM manipulation and Events section.
You might hear me saying this a lot because programming is not linear learning, you will eventually get it. Sometimes the basic understanding is good enough to move forward. So, it would really be not useful for you to know that
block scoped and
function scoped; because you don't even know what a block or what a function really is. We will learn about these eventually so stick with me, I'm sure that I can guide you on the right path. For now, what matters is that you should never use