JavaScript const keyword

In JavaScript, const is a keyword that is used to declare a variable whose value cannot be changed for the whole script.

For example

const hobby = 'Painting';
hobby = 'Dancing'; // This cannot be done.

The above code will through an error like this – TypeError: Assignment to constant variable.

Some points to be noted about const

  • The const keyword was declared in 2015 with ES6.
  • The variables defined with the const keyword cannot be redeclared.
  • The variables defined with the const keyword cannot be reassigned.
  • The variables defined with the const keyword have block scope(Just like the let keyword).

Variable declaration with const

A variable declared with const keyword must be assigned some value at the time of declaration.

Example

const hobby = 'Painting';

Incorrect approach

const hobby;
hobby = 'Painting';

Block Scope

Just like the JavaScript let keyword, variables declared with const has block scope.

const hobby = 'Painting';
// Here hobby is Painting

{
    const hobby = 'Dancing';
    // Here hobby is Dancing
}

// Here x is Painting

Redeclaration

Redeclaration of an existing var or let variable to const is not allowed in the same scope.

Example

var hobby = 'Painting';     // Allowed
const hobby = 'Dancing';   // Not allowed

Another Example

{
    let hobby = 'Music';     // Allowed
    const hobby = 'Coding';   // Not allowed
}

Another Example

{
    const hobby = 'Painting';   // Allowed
    const hobby = 'Singing';   // Not allowed
}

Redeclaring a variable with const, in another scope

Redeclaration of a variable with const, in another scope, is allowed.

Example

const hobby = 'Dancing';       // This is allowed

{
    const hobby = 'Painting';   // This is allowed
}

{
    const hobby = 'Music';   // This is allowed
}

Reassigning an existing const variable, in the same scope, is not allowed

Example

const hobby = 'Painting';     // Allowed
x = 'Dancing';           // Not allowed
var x = 'Music';       // Not allowed
let x = 'Travelling';       // Not allowed
const x = 'Coding';     // Not allowed

Another Example

{
    const hobby = 'Painting';     // Allowed
    x = 'Dancing';           // Not allowed
    var x = 'Music';       // Not allowed
    let x = 'Travelling';       // Not allowed
    const x = 'Coding';     // Not allowed
}

Hoisting in const

Using the var keyword, you can use the variable before it is declared. However, with the const keyword, this is not possible.

Example

country = 'India';
var country;

The above code is completely correct, but it is not possible with const.

Example

country = 'India';
const country;

The above code will throw an error saying – ReferenceError: Cannot access ‘country’ before initialization.

Object and arrays can also be declared using the const keyword. But, since we have not covered them as of now, we will see their declaration using const when we get their concepts.

Leave a Comment

Your email address will not be published. Required fields are marked *