An Interest In:
Web News this Week
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
- April 13, 2024
The const keyword
What is const?
The const
keyword was introduced in ES6 (2015), which is used to define a new variable in JavaScript. Variables that are defined with const
keyword cannot be redeclared, reassigned. These variables have a block scope {...}
.
Generally we use const
keyword to declare variables when we don't want to change the value of that variable throughout the program. However, if the constant or const
variable is an array or object then its items or properties can be updated or removed.
Syntax
const name = value;
Parameters
name
This is the name of the constant variable, which can be any legal identifier
value
This is the value of the constant variable, which can be any legal expression
Cannot be Reassigned
Variables that are defined with const
keyword cannot be reassigned a new value, else it will throw an error
,
const PI = 3.14;PI = 22; // this line will through an errorPI = PI + 5; // this line will through an error// this is the errorTypeError: Assignment to constant variable.
Must be Assigned
Variables that define with const
keyword must be assigned a value during declaration, otherwise it will throw an error
,
const PI;PI = 3.14;// this will throw an Syntax ErrorUncaught SyntaxError: Missing initializer in const declaration
Constant Arrays and Objects
The const
keyword is a little confusing because when we define a variable with const
keyword it does not define a constant value rather it defines a constant reference (Pointer) to a value.
Due to this reason we cannot reassign a constant value, constant array, constant object whereas we can change the elements or properties of constant arrays or objects.
Constant Arrays
So now we know that we can change the elements of the constant array,
// creating a constant arrayconst colors = ["Black", "Red", "Orange"];// changing the second element of the arraycolors[1] = "Blue"// adding elements to the constant arraycolors.push("Green");// resulting array["Black", "Blue", "Orange", "Green"];
But we cannot reassign the constant array, because it will throw an error
.
// creating a constant arrayconst colors = ["Black", "Red", "Orange"];// this will throw an Type Errorcolors = ["Red", "Blue", "Green"];// this is the errorTypeError: Assignment to constant variable.
Constant Objects
Just like constant arrays we can also change the properties of the objects,
// creating a constant objectconst colors = { r:"Red", g:"Green", b:"Brown" };// changing the value of third propertycolor.b = "Blue";// adding a new property to a constant arraycolor.a = "Alpha";// resulting constant objectcolors = { r:"Red", g:"Green", b:"Blue", a:"Alpha" };
But we cannot reassign the constant object, because it will throw an error
.
// creating a constant objectconst colors = { r:"Red", g:"Green", b:"Brown" };// this will throw an Type Errorcolors = { r:"Red", g:"Green", b:"Blue", a:"Alpha" };// this is the errorTypeError: Assignment to constant variable.
Block Scope
Just like let
keyword in JavaScript, variables that are declared with const
keyword have a block scope. Which means variables that are declared between curly braces {...}
are only accessible in between those curly braces.
const color = "Red";// Here color is Red{ const color = "Black"; // Here color is Black}// Here color is Red
Const Hoisting
Variables defined with const
are hoisted to the top, but not initialized.
Meaning: Using a const
variable before it is declared will result in a ReferenceError
:
// this is throw an ReferenceError: Cannot access 'color' before initializationconsole.log(color);const color= "Red";
Original Link: https://dev.to/moazamdev/the-const-keyword-lj5
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To