1. Using var
2. Using let
3. Using const
Using var
Variables are containers for storing data (values).In this example, x, y, and z, are variables, declared with the var keyword.
var x = 5;
var y = 6;
var z = x + y;
console.log('The value of z is: ' + z);
Using let
Variables defined with let cannot be re-declared. Variables defined with let must be declared before use. Variables defined with let have Block Scope.
let x = 'MindStick pvt ltd';
let x = 0;
// SyntaxError: 'x' has already been declared
var x = 2; // Allowed
let x = 3; // Not allowed
{
let x = 2; // Allowed
let x = 3 // Not allowed
}
{
let x = 2; // Allowed
var x = 3 // Not allowed
}
Using const
Variables defined with const cannot be re-declared. Variables defined with const cannot be re-assigned. Variables defined with const have block scope. Like fixed value.
const PI = 3.141592653589793;
PI = 3.14; // This will give an error
PI = PI + 10; // This will also give an error