In this chapter, we will learn about the const keyword in JavaScript. We will cover:
- What is
const? - Syntax
- Declaring Constants with
const - Block Scope
- Immutability
- Re-declaration Restrictions
constvsletvsvar
What is const?
const is a keyword used to declare variables in JavaScript that cannot be reassigned. Introduced in ES6 (ECMAScript 2015), const provides block scope and is used for variables whose values should not change throughout the program.
Syntax
const variableName = value;
constis the keyword.variableNameis the name of the variable.valueis the initial value assigned to the variable (required).
Declaring Constants with const
You can declare a constant using const followed by the variable name and an initial value. Note that const variables must be initialized at the time of declaration.
Example
const name = "Ramesh";
console.log(name); // Output: Ramesh
// const age; // This will cause an error because 'const' variables must be initialized
In the example above, the variable name is declared and initialized with the value "Ramesh". Attempting to declare a const variable without initializing it will cause an error.
Block Scope
Variables declared with const are block-scoped, meaning they are only accessible within the block they are declared in (e.g., inside {}). This is similar to let.
Example
if (true) {
const blockScoped = "This is block scoped";
console.log(blockScoped); // Output: This is block scoped
}
// console.log(blockScoped); // This will cause an error because blockScoped is not accessible outside the block
In the example above, the variable blockScoped is only accessible within the if block.
Immutability
Variables declared with const cannot be reassigned. However, this does not mean that the value itself is immutable. If the const variable holds an object or an array, the contents of the object or array can still be modified.
Example
const pi = 3.14;
console.log(pi); // Output: 3.14
// pi = 3.14159; // This will cause an error because 'pi' is a constant
const person = { firstName: "Ravi", lastName: "Kumar" };
person.firstName = "Ramesh"; // This is allowed
console.log(person); // Output: { firstName: "Ramesh", lastName: "Kumar" }
In the example above, attempting to reassign pi will cause an error, but modifying the contents of the person object is allowed.
Re-declaration Restrictions
Variables declared with const cannot be re-declared within the same scope, similar to let.
Example
const name = "Ramesh";
// const name = "Raj"; // This will cause an error because 'name' has already been declared
console.log(name); // Output: Ramesh
In the example above, re-declaring the variable name with const will cause an error.
const vs let vs var
Key Differences
-
Scope:
constis block-scoped.letis block-scoped.varis function-scoped.
-
Reassignment:
constvariables cannot be reassigned.letvariables can be reassigned.varvariables can be reassigned.
-
Hoisting:
- Variables declared with
constare hoisted but not initialized, leading to the temporal dead zone. - Variables declared with
letare hoisted but not initialized, leading to the temporal dead zone. - Variables declared with
varare hoisted and initialized withundefined.
- Variables declared with
-
Re-declaration:
constdoes not allow re-declaration within the same scope.letdoes not allow re-declaration within the same scope.varallows re-declaration within the same scope.
Example
function example() {
if (true) {
const constVariable = "const variable";
let letVariable = "let variable";
var varVariable = "var variable";
}
// console.log(constVariable); // This will cause an error because constVariable is block-scoped
// console.log(letVariable); // This will cause an error because letVariable is block-scoped
console.log(varVariable); // Output: var variable (varVariable is function-scoped)
}
example();
In the example above, constVariable and letVariable are not accessible outside the if block, while varVariable is accessible because it is function-scoped.
Conclusion
In this chapter, you learned about the const keyword in JavaScript. We discussed how to declare constants with const, its block scope, immutability, re-declaration restrictions, and the differences between const, let, and var. Understanding const is essential for writing modern and stable JavaScript code. In the next chapter, we will explore JavaScript operators and how to use them in your programs.