Javascript const

The const keyword in JavaScript is used to define variables that cannot be changed once they’re assigned a value. This prevents any modifications to the variable’s value.


Key Features of const:


1) Block Scope:


    Similar to let, const is block-scoped. This means that the variable declared with const is only accessible within the block in which it is defined.


Example
<!DOCTYPE html>
<html>
<body>
<h2>Javascript Const Block Scope</h2>
<p id="demo"></p>
<script>
if (true) {
const a = 5;
console.log(a); // Outputs: 5
}
console.log(a); // Error: a is not defined (because it's block-scoped)
</script>
</body>
</html>

Try it yourself

2) No Reassignment:


    A variable declared with const cannot be reassigned after it is initialized. Once a value is assigned, it remains constant.


Example
<!DOCTYPE html>
<html>
<body>
<h2>Javascript Const Reassignment</h2>
<p id="demo"></p>
<script>
const pi = 3.14;
pi = 3.14159; // Error: Assignment to constant variable
</script>
</body>
</html>

Try it yourself

3) Must Be Initialized:


    Unlike let or var, a const variable must be initialized at the time of declaration. You cannot declare a const variable without assigning it a value.


Example
<!DOCTYPE html>
<html>
<body>
<h2>Javascript Const Initialization</h2>
<p id="demo"></p>
<script>
const x; // Error: Missing initializer in const declaration
</script>
</body>
</html>

Try it yourself

4) Hoisting:


     Like let, const variables are hoisted to the top of their block, but they remain in the Temporal Dead Zone (TDZ) until they are initialized. This means you cannot access the variable before the point in the code where it is declared.


Example
<!DOCTYPE html>
<html>
<body>
<h2>Javascript Const Hoisting</h2>
<p id="demo"></p>
<script>
console.log(y); // ReferenceError: Cannot access 'y' before initialization
const y = 10;
</script>
</body>
</html>

Try it yourself

5) Constant Reference, Not Immutable Data:


    With objects and arrays, const makes the reference to the variable immutable, but the contents of the object or array can still be modified.


Example
<!DOCTYPE html>
<html>
<body>
<h2>Javascript Const Reference</h2>
<p id="demo"></p>
<script>
const person = { name: "John", age: 30 };
person.age = 31; // This works because the object itself is not constant, only the reference
console.log(person.age); // Outputs: 31
// But you cannot reassign the whole object:
person = { name: "Jane", age: 25 }; // Error: Assignment to constant variable
</script>
</body>
</html>

Try it yourself

6) No Re-declaration:

 

    Similar to let, you cannot re-declare a const variable in the same scope.

Example
<!DOCTYPE html>
<html>
<body>
<h2>Javascript Const Redeclaration</h2>
<p id="demo"></p>
<script>
const color = "blue";
const color = "red"; // Error: Identifier 'color' has already been declared
</script>
</body>
</html>

Try it yourself

Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.