Mastering JavaScript Const: The Ultimate Guide to Constants

Introduction to JavaScript Const

In JavaScript, the `const` keyword is used to declare a constant - a value that cannot be reassigned once it has been initialized. Constants are often used to store values that should never change throughout the program.

Properties:

  • Cannot be reassigned.
  • It’s Block Scope
  • It can be assigned to the variable on the declaration line.
  • It’s a Primitive value.
  • The property of a const object can be changed but it cannot be changed to a reference to the new object
  • The values inside the const array can be changed, it can add new items to const arrays but it cannot reference a new array.
  • Re-declaring of a const variable inside different block scopes is allowed.
  • Cannot be Hoisted.
  • Creates only read-only references to value.

Here's an example of declaring a constant:

const PI = 3.14159;

In this example, `PI` is a constant that has been assigned the value of `3.14159`. Once this constant has been declared and assigned, it cannot be reassigned to a different value.

Declaration and Initialization

To declare a constant, you use the `const` keyword followed by the constant name and the value you want to assign to it. Constants must be initialized at the time of declaration. Once initialized, constants cannot be redeclared or have their value changed.

Here's an example:

const name = 'John';
 
const age = 30;

In this example, `name` is a constant that is assigned the string value `'John'`, and `age` is a constant that is assigned the numerical value `30`.

Block Scope

Constants, like variables created with the `let` keyword, have **block scope**. This means they are only accessible within the block of code where they are defined. A block is typically defined by a set of curly braces `{}` used for loops, conditional statements, or function declarations.

Here's an example:

{
 
  const x = 10;
 
  console.log(x); // Output: 10
 
}
 
console.log(x); // Output: ReferenceError: x is not defined

In this example, `x` is a constant that is defined within the first block of code (enclosed by `{}`). It is accessible within that block and any nested blocks (if any). However, outside of that block, `x` is not defined.

Reassignment

Once a constant has been declared and assigned a value, it cannot be reassigned to a different value. Attempting to do so will result in an error.

Here's an example:

const country = 'Canada';
 
country = 'USA'; // Error: Assignment to constant variable

In this example, `country` is a constant that is initially assigned the value `'Canada'`. The following line tries to reassign `country` to the value `'USA'`, which results in an error.

Object Constants

When dealing with objects assigned to constants, the object itself cannot be reassigned (i.e., assigned a different object). However, the properties of the object can be modified.

Here's an example:

const person = {
 
  name: 'John',
 
  age: 30
 
};
 
person.name = 'Jane'; // Valid: Modifying a property
 
person = {};          // Error: Assignment to constant variable

In this example, `person` is a constant that is assigned an object with two properties: `name` and `age`. It is valid to modify the value of the properties (`person.name = 'Jane'`), but trying to reassign `person` to an empty object (`person = {}`) will result in an error.

Key Takeaways

  • The `const` keyword is used to declare constants in JavaScript.

  • Constants cannot be reassigned once they have been initialized.

  • Constants have block scope, meaning they are only accessible within the block of code where they are defined.

  • Constants can be used with objects, but the object itself cannot be reassigned - only its properties can be modified.

Comments

Popular posts from this blog

Limitations of JavaScript

'for in' Loop

What is JIT compiler? Is JavaScript compiled or interpreted or both?