A variable is a container that holds value. A variable name is basically used for referring value. For example
var n1=10
Here, n1 is variable. it's storing value 10.
In Javascript, there are 3 ways, you can declare a variable.
1) Var
2) let
3) const
the var keyword is used in an older version of javascript. the var statement declares a global-scoped or functional-scoped.
if you redeclaring a variable inside a block. its value will also change from outside of the block
var x = 5;
// Here x is 5
{
var x = 10;
// Here x is 10
}
// Here x is 10
Let keyword was introduced in ES6(2015), before let variable, we have only two types of variable scope, global scope, and functional scope. now we have one more scope i.e block scope.
{
let x = 10;
// Here x is 10
}
// variable x can't use here
const keyword was also introduced in ES6(2015).
The variable which is declared with const has block scope.
once the const variable is initialized, you cannot change its value. for example
const a=10;
a=3; // TypeError: Assignment to constant variable.