Skip to content

Instantly share code, notes, and snippets.

View parvezmrobin's full-sized avatar

Parvez M Robin parvezmrobin

View GitHub Profile
function colors(){
const colors = ['red', 'blue', 'green'];
let colorIndex = -1;
const iterator = {
next(){
colorIndex++;
colorIndex %= 3;
return {value: colors[colorIndex], done: false};
}
function range(from, to){
let currentValue = from;
const iterator = {
next(){
if (currentValue == to) {
return {value: undefined, done: true};
}
return {value: currentValue++, done: false};
const a = [1, 2, 3];
const itr = a[Symbol.iterator]();
itr.next() // {value: 1, done: false}
itr.next() // {value: 2, done: false}
itr.next() // {value: 3, done: false}
itr.next() // {value: undefined, done: true}
itr.next() // {value: undefined, done: true}
const user = {id: 1, username: 'thanos', email: '[email protected]'};
const {email: userEmail, ...restUser} = user;
console.log(userEmail, restUser) // "[email protected]" {id: 1, username: "thanos"}
const user = {id: 1, username: 'thanos', email: '[email protected]'};
const {id: userId, username, email: userEmail, canFly = false} = user;
const {canFly: canFly2, canFly: thanosCanFly = false} = user;
console.log(canFly, canFly2, thanosCanFly) // false undefined false
const avengers = [
{name: 'Iron Man', canFly: true},
{name: 'Captain America', canFly: false},
{name: 'Spider Man', canFly: false},
{name: 'Thor', canFly: true},
{name: 'Ant Man', canFly: false},
];
const [, {canFly: CaptainAmericaCanFly}, , {canFly: ThorCanFly}] = avengers;
console.log(CaptainAmericaCanFly, ThorCanFly); // false true
const avengers = ['Captain America', 'Iron Man', 'Thor', 'Hulk', 'Spider Man', 'Ant Man']
const lastIndex = avengers.length - 1;
const {[lastIndex]: theLastAvenger} = avengers;
console.log(theLastAvenger) // Ant Man
const a = [1, 2, 3, 4];
const {2: x, 3: z} = a;
console.log(x, z) // 3 4
const a = [1, 2, 3, 4];
const [, , z] = a;
console.log(z) // 3
const a = [1, 2, 3, 4];
const [x, y, z] = a;
console.log(x, y, z) // 1 2 3