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 :: javascript for validation 
Javascript :: javascript comment 
Javascript :: vue create component 
Javascript :: encrpting data in javascript 
Javascript :: js append html in div after 
Javascript :: string literals 
Javascript :: jquery from js 
Javascript :: how to wait for function to finish in JS 
Javascript :: passport js npm 
Javascript :: jq cheat sheet 
Javascript :: array.from 
Javascript :: passing ref to child component 
Javascript :: es6 import 
Javascript :: add value to object 
Javascript :: chrome dev tools console api 
Javascript :: string en javascript 
Javascript :: how to upload document cloddinary 
Javascript :: angular dinamic disabled 
Javascript :: javascript get data from hashmap 
Javascript :: eslint-disable-next-line multiple 
Javascript :: how to write to and read from text files line by line using javascript 
Javascript :: angular chart js Doughnut colors 
Javascript :: npm ln 
Javascript :: how to get selected value from between form tags in Angular 
Javascript :: how to delete an item on click in js 
Javascript :: nodejs json data serving 
Javascript :: intro to graphs with js 
Javascript :: react state scope 
Javascript :: how to access values from a form event callback 
Javascript :: understand frontend 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =