Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Spread Operator
- */
- // Add the elements of an existing array into a new array
- var certsToAdd = ['Algorithms and Data Structures', 'Front End Libraries'];
- var certifications = [
- 'Responsive Web Design',
- ...certsToAdd,
- 'Data Visualization',
- 'APIs and Microservices',
- 'Quality Assurance and Information Security'
- ];
- console.log(certifications);
- // Pass elements of an array as arguments to a function
- function addThreeNumbers(x, y, z) {
- console.log(x+y+z)
- }
- var args = [0, 1, 2, 3];
- addThreeNumbers(...args);
- // Copy arrays
- var arr = [1, 2, 3];
- var arr2 = [...arr]; // like arr.slice()
- arr2.push(4);
- console.log(arr);
- console.log(arr2);
- // Concatenate arrays
- var arr1 = [0, 1, 2];
- var arr2 = [3, 4, 5];
- //arr1.concat(arr2);
- arr1 = [...arr1, "JavaScript", ...arr2];
- console.log(arr1);
- /*
- Rest Operator
- */
- //Condense multiple elements into an array
- function multiply(multiplier, ...theArgs) {
- //theArgs behave as Array so we can apply all Array methods
- //But it has to be the LAST(!) one specified, since it captures the rest of your arguments. 👏
- return theArgs.map(function(element) {
- return multiplier * element;
- });
- }
- var arr = multiply(2, 1, 2, 3);
- console.log(arr)
- // Delete duplicates & convert to arrayConvert to Array
- let nums = [1, 2, 2, 3];
- console.log([...new Set(nums)])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement