var, let, const in JavaScript || Basics :)

Ritik Tyagi
2 min readMay 21, 2022

--

Variables in JavaScript

In JavaScript, var, let, and const are three ways of creating variables.. So, basically, variables are containers for storing data (storing data values).

There are three variables in JavaScript

  • var
  • let
  • const
  1. var : 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.
Example of Var variable.
  1. let : 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.
Example of let variable
  1. const : 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.
Example of const variable

I 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.

--

--