Skip to content

Instantly share code, notes, and snippets.

View parvezmrobin's full-sized avatar

Parvez M Robin parvezmrobin

View GitHub Profile
const a = [1, 2, 3, 4];
const [x, y, z] = a;
console.log(x, y, z) // 1 2 3
const user = {name: 'thanos', stones: {time: {color: 'green', achieved: true, from: 'Dr. Strange'} } }
const {stones: {time}} = user;
const {stones: {time: {achieved: isTimeStoneAchieved, color: timeStoneColor}}} = user;
console.log(time); // {color: "green", achieved: true, from: "Dr. Strange"}
console.log(isTimeStoneAchieved, timeStoneColor); // true "green"
const user = {id: 1, username: 'thanos', email: 'thanos@infinity.gauntlet'};
const {id: userId, username, email: userEmail} = user;
console.log(userId, username, userEmail) // 1 "thanos" "thanos@infinity.gauntlet"
console.log(id) // ReferenceError: id is not defined
const obj = {a: 1, b:2, c:3};
const {a, b} = obj;
console.log(a, b) // 1 2
const param = [Math.PI / 2];
console.log(Math.sin(...param)) // 1
const a = {
l: {
m: 1
}
}
const b = {...a}
b.l.m = 5;
console.log(a.l.m); // 5
const a = {
x: 1,
y: 1
}
const b = {
...a,
z: 1
}
const arr1 = [1, 2];
const arr2 = [3, 4];
const arr3 = [5, 6];
const arr2 = [...arr1, ...arr2, ...arr3];
console.log(arr2) // [1, 2, 3, 4, 5, 6]
const arr1 = [3, 4];
const arr2 = [1, 2, ...arr1, 5, 6];
console.log(arr2) // [1, 2, 3, 4, 5, 6]
function add(...args){
let sum = 0;
for(let value of args){
sum += value;
}
return sum;
}