const
The const
keyword is used to declare **block-scoped constants**. Once a value is assigned to a const
variable, it **cannot be reassigned**. It was introduced in ES6 (ECMAScript 2015) and is ideal for values that should not change.
const pi = 3.14; // pi = 3.14159 โ Error: Assignment to constant variable
Click the button to test how const
behaves:
const
:let
, it's accessible only within the block.{ const language = "JavaScript"; console.log(language); // JavaScript // language = "Python"; โ Error }
const
:You can't reassign the object/array, but you can change its content:
const user = { name: "Alice" }; user.name = "Bob"; // โ Allowed // user = { name: "Charlie" }; โ Error
Use const
by default. Only use let
if you know the value will change.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!