Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

rest parameters

// Before rest parameters, "arguments" could be converted to a normal array using:

function f(a, b) {

  let normalArray = Array.prototype.slice.call(arguments)
  // -- or --
  let normalArray = [].slice.call(arguments)
  // -- or --
  let normalArray = Array.from(arguments)

  let first = normalArray.shift()  // OK, gives the first argument
  let first = arguments.shift()    // ERROR (arguments is not a normal array)
}

// Now, you can easily gain access to a normal array using a rest parameter

function f(...args) {
  let normalArray = args
  let first = normalArray.shift() // OK, gives the first argument
}
Comment

rest parameters

const arr = [1, 2, 3];

let [x, ...z] =arr;
console.log(z);
console.log(x);
Comment

Rest parameter

//******************************************rest parameter
 const double = (...nums) => {//rest parameter is initialized by using three dots
//The numbers in the array called "result" is passed into the rest parameter. 

  console.log(nums);//each number in the rest parameter is ouput to the console
  return nums.map(num => num*2);//each number in the rest parameter is multiplied by 2 and then stored inside a map
};

const result = double(1,3,5,7,2,4,6,8);//creates a constant,called  "result". It then holds an array of numbers, in this example, and it could be an array of any length. 
//The numbers in parentheses after "double" are passed into the rest parameter also called "double"
console.log(result);//The final result is displayed, with each number having been multiplied by 2





// ************************************spread syntax (arrays)
//The spread syntax is similar to the 'rest parameter', one still uses the three dots to initialize it, but the syntax differs slightly, breaking the array up into its various components
const people = ['shaun', 'ryu', 'chun-li'];
//a good use for the spread array would be when we want to take the elements of one array, and spread them into another array (combine them)
const members = ['mario', 'luigi', ...people];
console.log(members);

//spread syntax (objects)
//again, it is similar to the spread syntax for arrays, but this time it is being applied to objects
const person = { name: 'shaun', age: 30, job: 'net ninja' };
//A NEW OBJECT is created, which copies the OBJECT called 'person' and then ALSO creates and fuses the 'personClone' array into the same object
const personClone = { ...person, location: 'manchester' };
console.log(person, personClone);
Comment

rest parameter syntax

foo(arg1, arg2, ...correct)
Comment

rest parameter

function f(a, b, ...theArgs) {
  // ...
}
Comment

rest parameter

function f(a, b, ...theArgs) {
  // ...
}
Comment

rest parameter

function f(a, b, ...theArgs) {
  // ...
}
Comment

rest parameter

function f(a, b, ...theArgs) {
  // ...
}
Comment

rest parameter

function f(a, b, ...theArgs) {
  // ...
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: Define Number Prop Vue 
Javascript :: Hide ReactTooltip after hover off 
Javascript :: express ubuntu ERR_CONNECTION_REFUSED 
Javascript :: random between min and max 
Javascript :: change node bash root 
Javascript :: canvas rounded corners 
Javascript :: how to use moment to compare time for calendar change color 
Javascript :: app.router.navigate reset framework7 
Javascript :: call dynamic var name javascript 
Javascript :: modal example react native 
Javascript :: JSON schema enumerated type 
Javascript :: js addeventlistener keyup android 
Javascript :: how to make html with jquery 
Javascript :: bouton scroll en haut 
Javascript :: puppeteer waitforselector 
Javascript :: react form hook trigger is not a function 
Javascript :: array from js 
Javascript :: mongoose connections 
Javascript :: callback vs return 
Javascript :: hide urls in .env in react app 
Javascript :: disable textarea angular 
Javascript :: node.js upload files 
Javascript :: update node two versions mac 
Javascript :: nodejs remove element from array 
Javascript :: js opposite of startswith 
Javascript :: pandas json_normalize column with json array 
Javascript :: js regex find text inside single quotes 
Javascript :: axios put request 
Javascript :: Get google maps getplace lat and long 
Javascript :: find element that has certain text javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =