Category: javascript
const key word
Published on 18 Feb 2026
Explanation
const is a keyword introduced in ES6 used
to declare variables with block scope. It creates
a constant reference that cannot be reassigned.
Code:
const name = "Praveen"; console.log(name);
Explanation
Variables declared with const are block scoped, meaning
they are only accessible within the block where
they are defined.
Code:
{
const x = 10;
console.log(x);
}
// console.log(x); // Error
Explanation
A const variable cannot be reassigned after its
initial declaration.
Code:
const age = 25; // age = 30; // Error: Assignment to constant variable
Explanation
const variables must be initialized at the time
of declaration.
Code:
// const count; // Error const count = 5;
Explanation
When using const with objects or arrays, the
reference cannot change, but the properties or elements
can be modified.
Code:
const user = { name: "Praveen" };
user.name = "Kumar";
console.log(user.name);
const numbers = [1, 2, 3];
numbers.push(4);
console.log(numbers);
Explanation
You cannot redeclare a const variable in the
same scope.
Code:
const id = 1; // const id = 2; // Error