Declaring a Constant Variable

The const keyword in JavaScript is primarily used for declaring constants. The value of a const variable cannot be reassigned after initialization.

Example: This example shows the declaration of some constant by using the const keyword.

Javascript
const PI = 3.14159;
console.log(PI); // Output: 3.14159
// Attempting to reassign the value of a
// const variable will result in an error
// PI = 3.14; // Error: Assignment to constant variable.

Output
3.14159

What does Const Do in JavaScript ?

In JavaScript, const is a keyword used to declare constants. Once assigned, the value of a constant cannot be changed. It provides a way to create read-only variables. Attempting to reassign a const variable will result in a syntax error.

These are the following ways to use const:

Table of Content

  • Declaring a Constant Variable
  • Using const with Object Literal
  • Using const with Arrays

Similar Reads

Declaring a Constant Variable

The const keyword in JavaScript is primarily used for declaring constants. The value of a const variable cannot be reassigned after initialization....

Using const with the Object Literal

const keyword in JavaScript is primarily used for declaring constants. The value of a const variable cannot be reassigned after initialization....

Using const with Arrays

const variables must be initialized at the time of declaration. const variables have block scope like let....