In JavaScript, const is a keyword used to declare variables with values that cannot be re-assigned after they are initially assigned. It stands for "constant." This means that once a value is assigned to a const variable, you cannot change that value throughout the rest of the code.

Here's a breakdown of const with its limitations, examples, and more:

Declaration and Initialization:

You declare a const variable using the const keyword followed by the variable name. You must also immediately assign a value to it at the time of declaration.

const pi = 3.14159;

 

Limitations:

1. Immutable Binding: The main purpose of const is to create an immutable binding. Once a value is assigned to a const variable, you cannot reassign a new value to it:

const x = 10;
x = 20; // This will result in an error because x is a constant.

2. Block-Scoped: Just like let, const is block-scoped, which means it is only accessible within the block it is defined in.

if (true) {
    const y = 5;
}
console.log(y); // This will result in an error because y is not accessible outside the block.

3. No Redeclaration: You cannot redeclare a variable that was declared using const.

const z = 15;
const z = 25; // This will result in an error because z is already declared.

Examples:

1. Array and Object Mutation: While a const variable itself cannot be reassigned, the contents of an array or object assigned to a const variable can still be mutated.

const numbers = [1, 2, 3];
numbers.push(4); // This is allowed and numbers becomes [1, 2, 3, 4].

const person = { name: 'Alice' };
person.age = 30; // This is allowed and person becomes { name: 'Alice', age: 30 }.

2. Function Declaration: When a function is assigned to a const variable, the variable itself is immutable, but the function can still be invoked and perform actions.

const greet = function(name) {
    console.log(`Hello, ${name}!`);
};
greet("Bob"); // Outputs: Hello, Bob!

 

When to Use const:

Use const when you want to ensure that a value remains constant and doesn't change accidentally. This is particularly useful for values that shouldn't be modified, such as mathematical constants, configuration values, or references to objects that you don't intend to replace.

const TAX_RATE = 0.10;
const config = {
    apiKey: "xyz123",
    endpoint: "https://api.example.com"
};

 

Remember that while const provides immutability to the variable binding, it doesn't necessarily make the data itself immutable (as demonstrated by the examples involving arrays and objects). If you need both the variable binding and the data to be truly immutable, you might need to use other techniques or data structures.

Latest Tutorial

Newly published tutorials

Laravel 10 - Tutorial on how to use jQuery to validate the form

Learn how to use jQuery in Laravel 10 to validate the form

Laravel 10 - Tutorial to create CRUD Application

Learn to create crud application using laravel 10. Step by step guidance will help to understand the concept of crud.

Laravel - How to upload file to AWS S3 Bucket

Explore how we can upload file using the laravel on Amazon S3 bucket.