Learn what const means in JavaScript with this simple guide for beginners. Understand how to use const with numbers, arrays, and objects, and why it's useful for writing safer code.
const in JavaScript?const is a way to create a variable in JavaScript, but one that cannot be changed later.
It stands for “constant” — something that stays the same.
Once you give a value to a const variable, you cannot change that value.
const name = "John";
const pi = 3.14;
console.log(pi); // 3.14
// You can't change it later:
// pi = 3.14159; // ❌ This will give an error
const colors = ["red", "blue"];
colors.push("green"); // ✅ You can add items
console.log(colors); // ["red", "blue", "green"]
// colors = ["yellow"]; // ❌ Not allowed: you can't assign a new array
const user = { name: "Alice", age: 25 };
user.age = 26; // ✅ You can change properties
console.log(user); // { name: "Alice", age: 26 }
// user = { name: "Bob" }; // ❌ Not allowed
const?