In JavaScript, a variable is a named storage location that holds a value, which can be any data type, such as numbers, strings, boolean, etc. It can be declared using keywords like var, let, or const.
1.var
var is function scoped, it can be re-declared and reassigned.
if(true){
var a = 10;
}
console.log(a);
//Output: 10
2.let
let is block scoped, it can be updated , but can't be redeclared in the same scope.
let x = 10;
if(true){
let x = 35;
console.log(x);
}
console.log(x);
//Output:
35
10
3.const
const is also block scoped, it can't be updated, used to store constant values
const pi = 3.14;
let r = 2;
let c = 2 * pi * r;
console.log(c);
//Output:
12.56
Top comments (0)