Const
Master this topic with zero to advance depth.
Expert Answer & Key Takeaways
Mastering Const is essential for high-fidelity technical architecture and senior engineering roles in 2026.
JavaScript Const
The
const keyword was introduced in ES6 (2015). It is used to declare variables that cannot be reassigned. In modern JavaScript engineering, const is the default choices for declaring variables.1. Cannot be Reassigned
A
const variable cannot be reassigned after it is initialized. Attempting to do so will result in an error.const PI = 3.14159;
PI = 3.14; // TypeError: Assignment to constant variable.
PI = PI + 10; // TypeError: Assignment to constant variable.2. Must be Initialized
JavaScript
const variables must be assigned a value when they are declared. You cannot declare a const variable without a value.// Correct way:
const PI = 3.14159;
// Incorrect (SyntaxError):
const PI;
PI = 3.14159;3. Constant Objects and Arrays
The keyword
const is a little misleading. It does NOT define a constant value. It defines a constant reference to a value.
Because of this, you can still change the elements of a constant array or the properties of a constant object.Constant Arrays
const cars = ["Saab", "Volvo", "BMW"];
// You can change an element:
cars[0] = "Toyota";
// You can add an element:
cars.push("Audi");
// But you can NOT reassign the array:
cars = ["Toyota", "Volvo", "Audi"]; // ERRORConstant Objects
const car = {type:"Fiat", model:"500", color:"white"};
// You can change a property:
car.color = "red";
// You can add a property:
car.owner = "Johnson";
// But you can NOT reassign the object:
car = {type:"Volvo", model:"EX90", color:"black"}; // ERROR4. Block Scope
Like
let, const variables have Block Scope. A variable declared within a block { } is not accessible from outside the block.const x = 10;
{
const x = 2; // This is a DIFFERENT x
}
// x is 10 hereTop Interview Questions
?Interview Question
Q:Does 'const' make a value immutable?
A:
No. 'const' creates a constant reference to a value. While you cannot reassign the variable to a new value, you can still modify the internal contents of objects or arrays (like adding properties or changing array elements).
?Interview Question
Q:Why must 'const' variables be initialized on declaration?
A:
Since a 'const' variable cannot be reassigned later, JavaScript requires an initial value at the moment of declaration. Without an initial value, the constant would remain undefined forever, which is not allowed.
?Interview Question
Q:When should you use 'const' over 'let'?
A:
Use 'const' for any variable that you do not expect to reassign. This is a best practice that signals intent to other developers and prevents accidental changes to important logic.
Course4All Engineering Team
Verified ExpertSenior Full-Stack Engineers & V8 Experts
Our JavaScript and engine-level content is developed by a collective of senior engineers focused on high-performance web architecture and 2026 standards.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.