Skip to content

Instantly share code, notes, and snippets.

View thamizhchelvan's full-sized avatar

Thamizhchelvan Govindan thamizhchelvan

View GitHub Profile
@thamizhchelvan
thamizhchelvan / array-copy-spread.js
Last active March 5, 2019 11:51
Array copy using Spread
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
@thamizhchelvan
thamizhchelvan / spread-normal-args.js
Created March 5, 2019 11:48
Spread with normal arguments
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"
@thamizhchelvan
thamizhchelvan / spread-es5.js
Created March 5, 2019 11:46
Spread using ES5
var sumresult = sum.apply(null,input);
console.log(sumresult); //prints 11
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
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"
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
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"}
//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
@thamizhchelvan
thamizhchelvan / object-destructuring-default-value.js
Created February 8, 2019 13:41
Object Destructuring with default value
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
@thamizhchelvan
thamizhchelvan / object-destructuring-simple.js
Last active February 8, 2019 13:40
basic object destructuring
//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;