What is Var,Let and Const in Javascript ?

Var, Let and Const are predefined sets of keywords used in the javascript programming language. in this article we are going to take a look at these keywords and how are they different from each other. These keywords in javascript are widely used and appreciated by programmers. Although they might be doing to same work of declaring the variable for further use in the programs, each of them has different scope and meaning.

What is Var in Javascript?

The Var statement in javascript is used for declaring variables. They can declare a variable for both global scope and function scope. Globally scoped variables can be accessed throughout the program whereas function scope variables are only available inside the declared block.

Var name = "SmallAcademy";
console.log(name); //SmallAcademy

Var keyword is used less and less every day in javascript due to more robust keywords such as let and const.

What is Let in Javascript?

The Let keyword in javascript is a more advanced version of the Var keyword. Even though both the keyword are used for variable declaration in javascript, the variable declared has a different meaning. Var as we know already declares a variable that is globally accessible or function scoped but variables declared using the Let keyword are pure local variables. These variables are accessible only inside the block statement or expression.

One of the major differences between Let and Var is, Var is processed even before the code in our program starts executing on the other hand Let is only processed when the parser evaluates it. Let’s take an example and understand the above statement.

function varfunc(){
var x = 1;
{// another block 
var x = 2; // same var with global address so x value is replaced by 2 
console.log(x); // 2 

console.log(x); // 2

// let example 

function letFunc(){
let x = 1;
{// another block 
let x = 2; 
console.log(x); // 2 

console.log(x); // 1 since the let variable is create different block x is not replaced by value 2 

What is CONST in Javascript?

The Const keyword holds a special meaning in javascript as it is used for declaring the constant variable. The variable created using const can not be redeclared in the scope it is created. The value of the variable can not be reassigned as well.  Const variable can be a globally scoped variable or block-scoped variable just like let statment. It creates a read-only reference to a value, therefore, cannot be changed later. And since the value can not be changed later, it needs to be initialized during the declaration of the const variable.

const age = 25;

age = 26; // this will throu an error



So that was the basic overview of what is Var const or Let in javascript. If you want to learn more about them, you can read Mozilla documentation for javascript here. if you have any queries or suggestions, please let us know in the comment section below.



We will be happy to hear your thoughts

Leave a reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.