JavaScript const
JavaScript const
JavaScript const keyword allows developers to declare variables that cannot be reassigned or redeclared, ensuring that their values remain constant throughout the code. This lesson covers everything you need to know about using const, from its syntax to practical examples.
JavaScript const Key Features:
- Cannot Be Redeclared: Once a
constvariable is defined, it cannot be declared again in the same scope. - Cannot Be Reassigned: You can’t change the value of a
constvariable after it’s been set. - Block Scope: Like
let,constis block-scoped, meaning it is only accessible within the{}block where it is defined.
When to Use JavaScript const?
- When you have values that should never change.
- For declaring constants, arrays, objects, functions, or regular expressions that won't be reassigned.
Declaring a const Variable
A const variable must be assigned a value at the time of declaration.
Syntax:
1const variableName = value;
Example:
1 2const PI = 3.14159; console.log(PI); // Output: 3.14159
Reassignment and Redeclaration
No Reassignment:
You cannot change the value of a const variable once it has been set.
1 2const PI = 3.14; PI = 3.15; // Error: Assignment to constant variable
No Redeclaration:
You cannot redeclare a const variable within the same scope.
1 2const user = "Alice"; const user = "Bob"; // Error: Identifier 'user' has already been declared
Block Scope
const variables are block-scoped, meaning they exist only within the block {} where they are declared.
Example:
1 2 3 4 5 6 7 8const score = 100; { const score = 50; console.log(score); // Output: 50 } console.log(score); // Output: 100
In this example, the score variable inside the block is separate from the one outside.
Best Practices for Using const
- Use
constby Default: Whenever possible, useconstfor variables that do not need to be reassigned. - Use
letfor Variables that Change: Only useletwhen you know the variable will be reassigned. - Avoid
var: Due to its function scope and hoisting issues, preferletandconstin modern JavaScript.
Frequently Asked Questions
const is used to declare variables that cannot be reassigned after their initial assignment. It ensures the value remains constant throughout the code.
Use const when you need to create variables that shouldn’t change, such as constants or objects that should not be reassigned.
const is block-scoped and prevents reassignment, while var is function-scoped and allows reassignment, which can lead to unexpected behavior in code.
The purpose of const is to provide immutability, ensuring that the variable's value cannot be changed once it’s been assigned, reducing potential errors.
Use const for values that won’t change, and let for variables that need to be reassigned or updated over time, ensuring better code maintainability.
Use const for function expressions when you want to ensure the function reference doesn't get reassigned, preserving its integrity throughout the code.
Still have questions?Contact our support team