JS Tutorial



JS CONST


JavaScript 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.

๐Ÿ“ Syntax:

const pi = 3.14;
// pi = 3.14159 โŒ Error: Assignment to constant variable

๐Ÿงช Try it Live:

Click the button to test how const behaves:

๐Ÿ“ฆ Features of const:

  • Block Scoped: Like let, it's accessible only within the block.
  • Cannot be reassigned: Once assigned, the value stays fixed.
  • Cannot be re-declared: Declaring again in the same scope throws an error.

๐Ÿง  Example:

{
  const language = "JavaScript";
  console.log(language); // JavaScript
  // language = "Python"; โŒ Error
}

๐Ÿ“ Objects and Arrays with 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

๐Ÿ“ Best Practice:

Use const by default. Only use let if you know the value will change.


๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review