[JavaScript] Difference between var, let and const keywords?




I want to discuss var, let and const keywords in detail.

Var

Javascript variables statement is used to declare a variable and Optionally we can initialize the value for that variable.

Example: var a =10;

  • Variables declarations are processed before the execution of the code.
  • The scope of a Javascript variable declared with var is its current execution context.
  • Scope of a Javascript variable declared outside the function is global
consider the following code snippet.


function nodeSimplified(){
  var a =10;
  console.log(a); // output 10
  if(true){
  var a=20;
  console.log(a); // output 20
  }
  console.log(a); // output 20
}

In the above code you can find, when the variable updated inside the if loop, the value of variable "a" updated 20 globally hence outside if loop value persists. It is something similar to the Global variable present in other languages need to use it with great care because there is a great possibility of overriding the existing value.

let

let statement declares a local variable in a block scope. Similar to var that we can optionally initialize the variable.

Example let a =10;

  • let statement allows you to create a variable with scope limited to a block on which it is used.
  • It is something similar to the variable we declare in other languages like Java, .net, etc.

consider the following code snippet.

function nodeSimplified(){
  let a =10;
  console.log(a); // output 10
  if(true){
  let a=20;
  console.log(a); // output 20
  }
  console.log(a); // output 10
}

It is almost the same behavior, we see in most of the language.

function nodeSimplified(){
  let a =10;
  let a =20; //throws syntax error
  console.log(a);
}

Error Message: Uncaught SyntaxError: Identifier 'a' has already been declared

however with var, it works fine.

function nodeSimplified(){
  var a =10;
  var a =20;
  console.log(a); //output 20
}

Scope will be well maintained with let statement and when using inner function let statement makes your code clean and clear.

Hope above examples will be helpful to understand var and let command and in case of any queries please write down in the comment section.

const

const statement values can be assigned once and it cannot be reassigned. scope of const statement works similar to let statement.

Example: const a =10;

function nodeSimplified(){
  const MY_VARIABLE =10;
  console.log(MY_VARIABLE); //output 10
}

As per usual naming standard declaring the const variable in capital letters. const a =10 will work the same way as above code. Naming standards should be followed to maintain the code for the longer run.

☝☝Question: What will happen when we try to reassign the const variable?

consider the following code snippet.

function nodeSimplified(){
  const MY_VARIABLE =10;
  console.log(MY_VARIABLE); //output 10
  MY_VARIABLE =20;         //throws type error
  console.log(MY_VARIABLE); 

}

Error Message : Uncaught TypeError: Assignment to constant variable.

The code will throw an error when we try to reassign the existing const variable.

If you enjoyed this article, please share with your developer friends.Thanks for reading.

Comments