Skip to main content

JavaScript Const

JavaScript Const

The const keyword, introduced in ES6 (ECMAScript 2015), is used to declare variables whose values cannot be reassigned after their initial assignment. It is block-scoped, providing more precise control over variable scope compared to the older var keyword. This article explores the characteristics, use cases, and best practices for using const.


Introduction to const

The const keyword declares variables that are read-only. Once a value is assigned to a const variable, it cannot be reassigned. However, it's important to note that while the reference to the value is constant, the value itself may still be mutable if it's an object or array.


Block Scope

const is block-scoped, which means it is only accessible within the block (enclosed by curly braces) where it is defined. This is in contrast to var, which is function-scoped or globally scoped.

// Block scope with const
function exampleFunction() {
  if (true) {
    const number = 10;
    console.log(number); // Output: 10
  }
  console.log(number); // Error: number is not defined (Out of scope)
}
exampleFunction();

Immutable References

Variables declared with const cannot be reassigned. This ensures that the reference to the value remains constant. However, if the value is an object or array, the contents can still be modified.

// Immutable reference with const
const name = 'Alice';
console.log(name); // Output: Alice

name = 'Bob'; // Error: Assignment to constant variable

Mutable Objects

While the reference to an object or array declared with const cannot be changed, the contents of the object or array can still be modified.

// Mutable object with const
const user = { name: 'Alice', age: 25 };
console.log(user.name); // Output: Alice

user.name = 'Bob'; // Allowed: modifying the property
console.log(user.name); // Output: Bob

user = { name: 'Charlie' }; // Error: Assignment to constant variable

Examples of Using const

Example 1: Constant Values

const PI = 3.14159;
console.log(PI); // Output: 3.14159

Example 2: Constant Arrays

// Modifying array elements
const numbers = [1, 2, 3];
numbers.push(4); // Allowed: modifying array elements
console.log(numbers); // Output: [1, 2, 3, 4]

numbers = [5, 6, 7]; // Error: Assignment to constant variable

Example 3: Constant Objects

// Modifying object properties
const person = { name: 'Alice', age: 25 };
person.age = 26; // Allowed: modifying object properties
console.log(person.age); // Output: 26

person = { name: 'Bob' }; // Error: Assignment to constant variable

Best Practices for Using const

  • Use const by Default: Prefer const for all variables that do not need to be reassigned. This provides a clear intent that the variable should not change.
  • Immutable References: Understand that const does not make objects or arrays immutable; it only ensures that the reference cannot be changed.
  • Block Scope: Leverage block scope to limit the variable's visibility and avoid potential issues with variable shadowing and scope leakage.

Conclusion

The const keyword provides a way to declare variables with immutable references, improving code clarity and reducing potential errors. By understanding its block scope and limitations regarding mutable values, you can write more robust and maintainable JavaScript code. Using const effectively can help you adhere to best practices and avoid common pitfalls in modern JavaScript development.

Comments