javascript has var keyword for variable declaration.
ES6 introduced let
and const
keywords for variables declaration.
This post explains about usage of the const
keyword and compares let
and const
with examples
es6 const keyword
const
is used to declare a constant variable that is assigned with a value.
Once assigned, Value never is changed, also called immutable variables.
const word = "words";
word="words1"; // error
const
variables are block scope similar to let keyword.
if the const
variable is not assigned value, It throws an error Uncaught SyntaxError: Missing initializer in const declaration
const word; // error
if you try to reassign the const variable with a new value, It throws Uncaught TypeError: Assignment to constant variable
const word = "words";
word="words1"; // error
The variables declared in block scope {}
are not accessible outside a block.
For example,
Below the program, the const
number is declared inside an if block, and accessible inside a block only.
It throws an error ReferenceError: number is not defined if constant is accessed outside a block scope
if (true) {
const number = 42;
}
console.log(number)
Difference between let and const in javascript?
Let’s see the difference between let
and const
-reassign the value For example, let variable can be reassigned with value
const
value is not reassigned and throws an error Uncaught TypeError: Assignment to constant variable.
// let example
let test="hello";
test="welcome";
console.log(test);
// const example throws an error
const message="hello";
message="welcome";
console.log(message);
let and const both are block-scoped only
these variables have a scope of immediate {}
let number = 3;
{
number = 9; //allowed
}
const word = "threee";
{
const word = "nine"; //allowed
}
- Order is important for let and const keyword
For variables with and const and let keyword, if the variable is used without declaring it, It throws an error.
Uncaught ReferenceError: Cannot access ’number’ before initialization".
console.log(word); // error
console.log(number); // error
const word = 11;
let number = 22;
- let and const variables are hoisting
One more difference in hoisting.
Variables declared with const
and 1 keyword assigned with undefined before it executes or uses. These are also called variables hoisted.
It throws an error Uncaught ReferenceError: Cannot access ‘variable’ before initialization
In this program, the let
and const
variable is not initialized and accessed, it is in a temporal dead zone
function myfunction() {
console.log(variable); // error Uncaught ReferenceError: Cannot access 'variable' before initialization
const variable = "hello";
console.log(variable);
}
myfunction();
using let variable
function myfunction() {
console.log(variable); // error Uncaught ReferenceError: Cannot access 'variable' before initialization
let variable = "hello";
console.log(variable); //
}
myfunction();
let
andconst
variable is not added to the window global scope
In this example, accessing let
and const
variables with window scope returns undefined.
const word = "words";
let number = "number";
console.log(number); // number
console.log(word); // words
console.log(window.number); // undefined
console.log(window.word); // undefined