JavaScript const

The const keyword has all the properties that are the same as the let keyword, except the user cannot update it and have to assign it with a value at the time of declaration. These variables also have the block scope. It is mainly used to create constant variables whose values can not be changed once they are initialized with a value.

Example 1: This code tries to change the value of the const variable.

Javascript
const a = 10;
function f() {
    a = 9
    console.log(a)
}
f();

Output:

TypeError:Assignment to constant variable.

Example 2: This code explains the use of the const keyword to declare the JavaScript objects.

Javascript
const a = {
    prop1: 10,
    prop2: 9
}

// It is allowed
a.prop1 = 3

// It is not allowed
a = {
    b: 10,
    prop2: 9
}

Output:

Uncaught SyntaxError: Unexpected identifier

Difference between var, let and const keywords in JavaScript

The keywords var, let, and const in JavaScript define the variable scope and behavior. The var keyword has function scope and is hoisted. The let and const keywords have block scope, with const requiring an initial value and preventing reassignment.

Similar Reads

JavaScript var keyword

The var is the oldest keyword to declare a variable in JavaScript. It has the Global scoped or function scoped which means variables defined outside the function can be accessed globally, and variables defined inside a particular function can be accessed within the function....

JavaScript let keyword

The let keyword is an improved version of the var keyword. It is introduced in the ES6 or EcmaScript 2015. These variables has the block scope. It can’t be accessible outside the particular code block ({block})....

JavaScript const

The const keyword has all the properties that are the same as the let keyword, except the user cannot update it and have to assign it with a value at the time of declaration. These variables also have the block scope. It is mainly used to create constant variables whose values can not be changed once they are initialized with a value....

Differences between var, let, and const

varletconstThe scope of a var variable is functional or global scope.The scope of a let variable is block scope.The scope of a const variable is block scope.It can be updated and re-declared in the same scope.It can be updated but cannot be re-declared in the same scope.It can neither be updated or re-declared in any scope.It can be declared without initialization.It can be declared without initialization.It cannot be declared without initialization.It can be accessed without initialization as its default value is “undefined”.It cannot be accessed without initialization otherwise it will give ‘referenceError’.It cannot be accessed without initialization, as it cannot be declared without initialization.These variables are hoisted.These variables are hoisted but stay in the temporal dead zone untill the initialization.These variables are hoisted but stays in the temporal dead zone until the initialization....