Var, Let and Const

var is function scoped whereas let is block scoped

Posted by on December 10, 2017

Var, Let and Const

var is function scoped whereas let is block scoped

Var, let & const - manoj singh negi

let and const keywords are introduced in ES6 and brought some improvements to the Javascript language with them let’s explore what they actually are.

Const

Let’s define const keyword first, const introduced in javascript to declare constant variables. A variable declared using const cannot be reassigned.

For example

As you can see we cannot reassign a but there is more to the definition of const, we cannot reassign a but we can change its value.

For example

So the question arises when to use const? In my opinion, if you are going to write code as we written above don’t use const use var or let because if you are going to use const and then change the value of the variable it will affect the readability of your code. Logically and syntax-wise the above code is totally fine but to a person who will be reading this code it will create a lot of confusion. Why define a variable with const if you are going to change its value.

Myself, I use const when I want to restrict myself and developers working with me from changing the value of a variable otherwise I use let which makes it clear that we can reassign or modify the variable.

Var & Let

The main difference between let and var is that variable declared with let is block scoped and variables declared with var is function scoped.

NOTE: You can start block scope by typing { } 

What that’s mean ? let’s take a look at example

What do you think the output of this function will be ? 2 or a ReferenceError ?

If you answer is 2 you are correct. Here is another example but this time we will be using let

If you will run this on your console you will get a ReferenceError. Did you notice what happened there?

In our first example with var, the x can be accessed anywhere inside the function because variables declared with var are function scoped. In our second example with let we got a ReferenceError because we are trying to access x outside the block we defined it.

Let’s take a look at another example

In letTest function, our outside declared x will not interfere with the inside declared x and vice versa. Both x is defined in block scope and hence don’t affect each other. When we console the outside x it outputs 1.

In varTest function, when we define the inside x it overwrites the outside x because both variables are function scoped and only one x can be defined inside one scope. When we output x it outputs 2.

Also, take notice even when inside x overwrites the outside x we don’t get a warning that you are re-declaring x.

Javascript will not give you any warning or error if you re-declare a variable but if you declare a variable with let you can’t re-declare it.

So here is the difference between var, let and const. If I miss something or you want to suggest something about this topic feel free to address that in comments.

Hi, My name is Manoj Singh Negi. I am a Javascript Developer and writer sharing my world view with everyone. Join my quest by following me at Twitter or Medium.

Loved this article ? Here is more for you.

1. The fundamental of everything in life

2. Why credibility matters?

3. What leadership actually means

4. Fear not, Lead that’s the best way

5. What I have learned so far in my journey of life

Peace.


Hey there. Follow along.