Features of ECMAScript 2015 also known as ES6 ↓
var a = "Avail Every where";
{ // this is starting of a block
let b = "Avail in block only";
console.log(a);
console.log(b);
} // this is ending of a block
console.log(a);
console.log(b); // give error
JavaScript program to print table for given number (8)
let tableOf = 8;
for(let num = 1; num <= 10; num++) {
console.log(`${tableOf} * ${num} = ${tableOf * num}`);
}
function mult(a, b = 5){
return a * b;
}
console.log(mult(3)); // prints 15
const myBioData = ['vinod', 'thapa', 26];
// normally and lengthy way
// let myFName = myBioData[0];
// let myLName = myBioData[1];
// let myAge = myBioData[2];
// using destructuring method
let [myFName , myLName, myAge] = myBioData;
console.log(myAge);
// we can add values too
let [myFName, myLName, myAge, myDegree = "MCS"] = myBioData;
console.log(myDegree);
const myBioData = {
myFName = 'vinod',
myLName = 'thapa',
myAge = 26
}
let age = myBioData.age;
let myFName = myBioData.myFName;
let {myFName, myLName, myAge, myDegree = "MCS"} = myBioData;
console.log(myLName);
Normal way of writing function
function sum() {
let a = 5; b = 6;
let sum = a + b;
return `The sum of the two number is ${sum}`;
}
console.log(sum());
Converting above funtion to Fat Arrow Function
const sum = () => `The sum of the two number is ${(a = 5) + (b = 6)}`;
console.log(sum());
const colors = ['red', 'green', 'blue', 'white', 'black'];
const myColors = ['red', 'green', 'blue', 'white', 'black', 'yellow', 'gray'];
Now instead we can use spread (...)
const colors = ['red', 'green', 'blue', 'white', 'black'];
const myFavColors = [...colors, 'yellow', 'gray'];
const colors = ['red', 'green', 'blue', 'white', 'black'];
console.log(colors.includes('green')); // true
console.log(2**2);
String padding
Object.values()
Object.entries()
const message = "my name is vinod";
console.log(message);
console.log(message.padStart(5));
console.log(message.padEnd(10));
const person = { name: 'Fred', age: 87 };
// console.log( Object.values(person) );
const arrObj = Object.entries(person);
console.log(Object.fromEntries(arrObj));
const person = { name: 'Fred', age: 87, degree : "mcs" };
const sPerson = { ...person };
console.log(person);
console.log(sPerson);
Array.prototype.{flat, flatMap}
Object.fromEntries()
let oldNum = Number.MAX_SAFE_INTEGER;
// console.log(oldNum);
// console.log( 9007199254740991n + 12n );
const newNum = 9007199254740991n + 12n;
console.log(newNum);
console.log(typeof newNum);
const foo = null ?? 'default string';
console.log(foo);