DEV Community

gaurbprajapati
gaurbprajapati

Posted on

Const Keyword in javascript

In JavaScript, the const keyword is used to declare variables that have block scope and whose values cannot be reassigned after they are initialized. Here's an explanation of how const works:

1. Block Scope:
Variables declared with const have block scope, just like variables declared with let. They are only accessible within the block in which they are defined. This includes if statements, loops, and any other block of code. For example:

   function greet() {
     if (true) {
       const message = "Hello";
       console.log(message);
     }
     console.log(message); // Throws ReferenceError: message is not defined
   }

   greet(); // Output: Hello
Enter fullscreen mode Exit fullscreen mode

In this example, the message variable is declared with const inside the if block. It is accessible within the block and can be used for logging. However, trying to access the variable outside the block will result in a ReferenceError since it is not defined in that scope.

2. Value Assignment:
Variables declared with const must be assigned a value at the time of declaration. Once assigned, the value cannot be changed or reassigned. For example:

   const PI = 3.14;
   PI = 3.14159; // Throws TypeError: Assignment to constant variable.
Enter fullscreen mode Exit fullscreen mode

In this case, PI is assigned the value 3.14 when it is declared with const. Any attempt to reassign a new value to PI will result in a TypeError because const variables are read-only.

3. Immutable Values:
const variables are not completely immutable. While you cannot reassign the variable itself, if the variable holds a reference to an object or an array, the properties or elements of that object or array can still be modified. For example:

   const person = {
     name: "John",
     age: 25,
   };

   person.age = 30; // Valid, the properties of the object can be modified
   person = {}; // Throws TypeError: Assignment to constant variable.
Enter fullscreen mode Exit fullscreen mode

In this example, the person variable is declared with const and initially assigned an object. Although you cannot reassign a new object to person, you can modify the properties of the existing object.

4. Hoisting and Temporal Dead Zone (TDZ):
Variables declared with const are also not hoisted to the top of their scope like variables declared with var. They are subject to the temporal dead zone (TDZ), which means they cannot be accessed before they are declared. For example:

   console.log(name); // Throws ReferenceError: name is not defined
   const name = "John";
   console.log(name); // Output: John
Enter fullscreen mode Exit fullscreen mode

In this case, attempting to access the name variable before it is declared will result in a ReferenceError. The variable needs to be declared before it can be used.

The const keyword provides a way to declare variables that are intended to be read-only and not reassigned. It helps enforce immutability and ensures that certain values remain constant throughout the execution of the program. It is recommended to use const whenever you know that a variable's value should not be changed after initialization.

Top comments (0)