The const keyword in JavaScript is a modern way to declare variables, introduced in (ES6). It is used to declare variables whose values need to remain constant throughout the lifetime of the application.
const is block-scoped, similar to let, and is useful for ensuring immutability in your code. Unlike let, the primary feature of const is that it cannot be reassigned once it has been initialized.
Syntax
const variable = value;
const is used to declare a variable and assign a value to it.
After initialization, the value of a const variable cannot be reassigned.
Key Features of const
1. Block Scope
Variables declared with const are block-scoped, which means they are accessible only within the block, statement, or expression in which they are defined.
The variable y is declared with const and assigned a value of 20.
Attempting to reassign y to 30 results in a TypeError because const variables cannot be reassigned.
3. Must Be Initialized
Unlike let, a const variable must be initialized at the time of declaration. Declaring a const variable without assigning a value will throw a SyntaxError.
The variable z is declared with const, but it is missing an initial value.
This causes a SyntaxError because const requires an initializer (a value) at the time of declaration.
4. Immutable Binding, Not Value
const makes the variable binding immutable, but if the value is an object or array, you can still modify its properties or contents.
The code shows that although obj and arr are declared with const, their properties and elements can still be modified.
The objectβs property name and the arrayβs elements are successfully changed, demonstrating that const applies only to the binding, not the contents.
5. No Redeclaration
Variables declared with const cannot be redeclared within the same scope, similar to let.
greet is declared with const, so it can't be reassigned, causing a TypeError.
The first call logs "Hello, world!", but the reassignment won't happen due to the error.
8. Integration with Modern JavaScript
const aligns with modern JavaScript practices, supporting features like destructuring and modules for cleaner, maintainable code.
The code uses destructuring to extract the name and age properties from the object and assign them to variables.
It then logs the values of name ("Meenal") and age (28) to the console.
Interesting Facts about const
Block-scoped: Like let, const is block-scoped, meaning it is only accessible within the block, statement, or expression where it is defined.
Immutable Binding: A const variable cannot be reassigned once it is initialized, providing a guarantee of immutability for the binding (not the value itself).
Objects and Arrays: While the binding of a const variable is immutable, the contents of objects and arrays assigned to const can still be modified.
Hoisting: Variables declared with const are hoisted to the top of their block but cannot be accessed until they are initialized, resulting in a "temporal dead zone" error if accessed before assignment.
Used with Destructuring: const can be used in object and array destructuring, allowing you to easily unpack values from an object or array into individual variables.