Variable
In JavaScript, a variable is a named container used to store data values
- Using var
- Using let
- Using const
Var:
The var keyword is used to declare a variable. It was the original way to declare variables in JavaScript and has specific characteristics
var x = 10;
var y= 20;
console.log(x);
// Output: 10
Let
- The let keyword was introduced in ES6 (2015)
- Variables declared with let have Block Scope
- Variables declared with let must be Declared before use
- Variables declared with let cannot be Redeclared in the same scope
{
let x = 2;
Console.log(x)
}
// x can NOT be used here
Const
- The const keyword was introduced in ES6 (2015)
- Variables defined with const cannot be Redeclared
- Variables defined with const cannot be Reassigned
- Variables defined with const have Block Scope
{
const x = 10;
console.log(x);
// Output: 10
}
console.log(x);
// ReferenceError: x is not defined
Top comments (0)