Nerd For Tech
Published in

Nerd For Tech

Var, Let, and Const in JavaScript

In JavaScript, var, let, and const are three ways of creating variables. Here, we will talk about the scope and difference between these three ways.

Bit of history on variable declaration

At the beginning of JavaScript, there was one way to declare a variable and, that is using the var keyword. Then let and const are introduced in ES6 but couldn't use it right away. Now, all the major browsers compatible with the let and const syntax, and most of the developers use let and const nowadays.


Variables declared using var keyword scoped to the current execution context. This means If they are inside a function we only can access them inside the function. and If they are not they are part of the global scope which we can access anywhere. look at the example below to better understanding.

Here, Mango is scoped to function scope and Apple belongs to the global scope. If we try to access a global variable it is possible. but if we try to access a function scoped variable it is not possible. look at another example below.

One of the issues with using the var keyword is they can be redeclared inside the same scope. This will brings up some serious issues if we declare another variable using the same name inside the same scope, the new variable will replace the old one. var can be updated as well.

And another issue with var is these variables are not block-scoped which means if we have conditions statements those are not scoped to that statement but to the entire function or to the global scope.


This is the improved version of var declarations. Variables declaration using this way eliminates all the issues that we discussed earlier. let creates variables that are block-scoped. Also, they can not be redeclared and can be updated. The below example shows it is the best choice to use let than var.

Check out the below example to understand more about the behavior of let in the block scope.


Const variables are cannot be updated or redeclared. This way is used to declare constants. Same as the let declarations const declarations are block-scoped. Unlike var and let, If we are using const to declare a variable that must be initialized.

If we use const when object creating we can still update properties inside that object. Refer to the below example for better understanding.

I personally prefer to use let and const over var and use const to declare constant variables and always use let to declare variables if it is not a constant.




NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit

Recommended from Medium

[Action required] Your Trial has Expired — Thu Dec 16 2021

Caching REST calls when offline

Module Federation In Vuejs Applications

Abstractions in Action: ORM’s and Javascript

MVC Website from .NET Core 6 Empty Project

NodeJS With React — A Deadly Combination To Develop Web Apps

nodejs with react to build web apps

Angular course: Interpolation and expressions

ES6 Template Literals or Template Strings

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thushara Thiwanka

Thushara Thiwanka

Full-stack Developer Intern | Software Engineering Undergraduate

More from Medium

Most useful Array methods in Javascript ( filter  — part 2)

Javascript roadmap first part

What is Reactive JavaScript?

How To Make API Call-In Javascript