Hackforge Academy

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

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community