AdsReg.com Magazine Logo
📌 Press Ctrl+D to bookmark this page.

Const

Whats const?

A const variable cannot be reassigned

You have to know:
Variables defined with const cannot be Redeclared.

Variables defined with const cannot be Reassigned.

Variables defined with const have Block Scope.

 

Const Samples Overview?

const aq = 4.44267568;
aq = 1.44; // This will give an error
aq = aq + 70; // This will also give an error
const PI = 3.14159265359; //Correct
const PI; //incorrect
PI = 88.6546762; //incorrect
// You can create a constant array:
const cars = ["Tesla", "Jeep", "BMW"];

// You can change an element:
cars[0] = "Toyota";

// You can add an element:
cars.push("Audi");
const cars = ["Tesla", "Jeep", "BMW"];

cars = ["Toyota", "Volvo", "Audi"]; // ERROR
// You can create a const object:
const car = {type:"Fiat", model:"100", color:"pink"};

// You can change a property:
car.color = "red";

// You can add a property:
car.owner = "Bill";

const car = {type:"Fiat", model:"200", color:"Red"};

car = {type:"Volvo", model:"EX60", color:"pink"}; // ERROR

Sample1: Const

JavaScript/005-Const/001

Sample2: Const

JavaScript/005-Const/002

Sample3: Const

JavaScript/005-Const/003

Leave a Comment

Your email address will not be published. Required fields are marked *

AdsReg Magazine We would like to show you notifications for the latest news and updates. You can disable it when you want.
Dismiss
Allow Notifications