Skip to content

Instantly share code, notes, and snippets.

@thisiskhan
Created June 9, 2021 08:23
Show Gist options
  • Save thisiskhan/27ad87a7b7367dccf40c46060d657156 to your computer and use it in GitHub Desktop.
Save thisiskhan/27ad87a7b7367dccf40c46060d657156 to your computer and use it in GitHub Desktop.
// Spread & Rest Operators
/*
Spread: Used to split up arrow elements OR object properties.
const nnewarray = [...oldArray, 1,2]
const newObject = {...oldObject, newProp:5}
Rest: Used to merge a lost of functions argumnets into array.
function sortArray(...arg){
return args.sort()
}
*/
// Spread Operator.
const numbers = [1, 2, 3 ];
const newNumbers = [...numbers, 4];
console.log(newNumbers);
/*
Output: [ 1, 2, 3, 4 ]
*/
const person = {
name : "Raza Khan"
}
const newPerson = {
...person,
age : 18,
}
console.log(newPerson.name, newPerson.age);
/*
Output: Raza Khan 18
*/
// Rest Operator
const filter = (...args) => {
return args.filter(el => el === 1);
}
console.log(filter(1, 2, 3));
/*
Output: [ 1 ]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment