This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let fruits = ["Apple", "Mango", "Orange"]; | |
let newfruits = [...fruits]; //in ES5 it should be like this: var newfruits = fruits.slice(0); | |
console.log(newfruits);//prints "Apple", "Mango", "Orange"] | |
//modifying the new array won't impact the the source array | |
newfruits.push("Grape"); | |
console.log(fruits);//prints "Apple", "Mango", "Orange"] | |
console.log(newfruits);//prints "Apple", "Mango", "Orange","Grape"] | |
//this is not the case with copying multi-dimensional arrays |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const calculateMarks = (student_name, mark1, mark2, mark3, mark4, mark5) => { | |
return student_name + "'s total marks: " + (mark1+mark2+mark3+mark4+mark5); | |
} | |
let alexmarks = calculateMarks("Alex",...[45,67,89,68,56]); | |
console.log(alexmarks);//printsc "Alex's total marks: 325" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sumresult = sum.apply(null,input); | |
console.log(sumresult); //prints 11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sum = (a,b,c) => (a+b+c); | |
let input = [3,2,6]; | |
let result = sum(...input); //this is equal to invoking the sum function as : sum(3,2,6); | |
console.log(result);//prints 11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let {blue,others:{yellow,orange},red} = {blue:"Grape",others:{yellow:"Mango",orange:"Orange"},red:"Apple"}; | |
console.log(blue); //prints "Grape" | |
console.log(yellow); //prints "Mango" | |
console.log(orange); //prints "Orange" | |
console.log(red); //prints "Apple" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let {size=38,color='green',price,discount} = {size:27,color:undefined,price:23.25,discount:null}; | |
console.log(size); //prints 27, overridden from default 38 | |
console.log(color); //prints green as no value provided in right side | |
console.log(price); //prints 23.25 as no default value | |
console.log(discount); //prints null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let {red,yellow,...others} = {red:"Apple",yellow:["Banana","Mango"],blue:"Grape",orange:"Orange"}; | |
console.log(red);//prints "Apple" | |
console.log(yellow); //["Banana","Mango"] | |
console.log(others); //{blue:"Grape",orange:"Orange"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//new variable name | |
let {red:redFruit,yellow:yellowFruit,orange} = {red:"Apple",yellow:"Banana",orange:"Orange"}; | |
console.log(redFruit); //prints "Apple" | |
console.log(yellowFruit); //prints "Banana" | |
console.log(orange); //prints "Orange" | |
//this can have default value as well |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let {size=38,color='green',price,discount} = {size:27,color:undefined,price:23.25,discount:null}; | |
console.log(size); //prints 27, overridden from default 38 | |
console.log(color); //prints green as no value provided in right side | |
console.log(price); //prints 23.25 as no default value | |
console.log(discount); //prints null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//basic object destructuring | |
//with variable declaration on same line | |
let {google,microsoft,amazon} = {google:"gcp",microsoft:"azure",amazon:"aws"}; | |
console.log(google); //prints gcp | |
console.log(microsoft); //prints azure | |
console.log(amazon); //prints aws | |
//with previously declared variables | |
let mobiles,laptops,tablets; |
NewerOlder