xxxxxxxxxx
const b = 1; // this variable unique and can't change it.
xxxxxxxxxx
const obj = {
name: "Fred",
age: 42,
id: 1
}
//simple destructuring
const { name } = obj;
console.log("name", name);
//assigning multiple variables at one time
const { age, id } = obj;
console.log("age", age);
console.log("id", id);
//using different names for the properties
const { name: personName } = obj;
console.log("personName", personName);
Run code snippet
xxxxxxxxxx
// var declares a variable, meaning its value will vary.
// const declares a constant, meaning its value will remain
// consistant and not change.
// If your variable changes throughout the program or website,
// declare it using a var statement.
// Otherwise, if its value does not change, declare it using
// a const statement.
const myConst='A const does not change.';
var myVar='A var does change.';
var myVar=2;
xxxxxxxxxx
const PI = 3.141592653589793;
PI = 3.14; // This will give an error
PI = PI + 10; // This will also give an error
xxxxxxxxxx
const age; // errror as const cannot be kept un-initialized;
const age = 20 ;
const age = 21 , // error as once declared const variable cann't be
// re-declared in same scope or different scope.
xxxxxxxxxx
// var makes The Value changeable
// const makes The Value Unchangeable
// let makes The Value changeable in block scoped
// DON'T DO THIS
const dellor = 6;
dellor = 4; // ERROR
console.log(dellor);
// NOR THIS
let dellor = 6;
let dellor = 4; // ERROR
console.log(dellor);
// DO THIS INSTEAD
var dellor = 6;
dellor = 4; // The Value will change
console.log(dellor);