Rest Spread
๐ ALL INFORMATION CLICK HERE ๐๐ป๐๐ป๐๐ป
Rest Spread console . log ( multiply ( 1 , 2 , 3 , 4 , 5 )) ; ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX function multiplyAll ( ... args ) { ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX function welcomeSite ( siteName , bookName , ... bookTitles ) { console . log ( ' Welcome to ' + siteName + 'โs ' + bookName + ' book' ) ; // Welcome to W3Docsโs JS book // the rest go into bookTitles array console . log ( bookTitles [ 0 ]) ; // Arrays console . log ( bookTitles [ 1 ]) ; // Functions console . log ( bookTitles . length ) ; // 2 ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX function f(arg1, ...rest, arg2) { // arg2 after ...rest!!!! // error } console . log ( arguments . length ) ; showSiteBooks ( "Javascript" , "Git" ) ; ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX showSiteBooks ( "Javascript" , "Git" ) ; ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX let showArgs = ( ) => console . log ( arguments [ 0 ]) ; ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX console . log ( Math . max ( ... arr )) ; // 8 (spread makes array into a list of arguments) ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX let arr1 = [ 2 , - 3 , 5 , 4 ] ; let arr2 = [ 9 , 2 , - 7 , 2 ] ; console . log ( Math . max ( ... arr1 , ... arr2 )) ; // 9 ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX let arr1 = [ 2 , - 3 , 5 , 4 ] ; let arr2 = [ 9 , 2 , - 7 , 2 ] ; console . log ( Math . max ( 3 , ... arr1 , 6 , ... arr2 , 18 )) ; // 18 ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX let merged = [ 1 , ... arr , 3 , ... arr2 ] ; console . log ( merged ) ; // 1,5,4,9,3,6,8,12 (1, then arr, then 3, then arr2) ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX console . log ([ ... str ]) ; // W,3,D,o,c,s ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX // Array.from converts an iterable into an array console . log ( Array . from ( str )) ; // W,3,D,o,c,s ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Quizzes HTML Basic CSS Basic Javascript Basic PHP basic ES6 Basic TypeScript Basic Angular Basic React Basic Sass Basic Vue.js Basic Git Basic SQL Basic Snippets How To NodeJs How To Linux How To AngularJs How To PHP How To HTML How To CSS How To Symfony How To Google Maps How To Git How To Apache How To JavaScript How To Java How To Vue.js Our Books Learn HTML Learn CSS Learn Git Learn Javascript Learn PHP Exercises HTML JavaScript Git CSS PHP Our Tools Typing Test Color Picker HTML Encoder HTML Editor CSS Maker Password Generator Base 64 Code Diff String Functions String Length Calculator String to MD5 Hash Generator String to Sha256 Hash Generator String Reverse URL Encoder URL Decoder Base 64 Encoder Base 64 Decoder Extra Spaces Remover String to Lowercase Converter About Us Privacy Policy for W3Docs Company ยฉ W3docs. All rights reserved. JavaScript has a range of helpful features allowing developers to work with arrays and function parameters much effortlessly. In this chapter, the rest parameters and spread syntax are covered. The rest parameter gives you a robust way to work with an indefinite quantity of parameters. For better understanding what rest parameter is, letโs start from the beginning. You can call a function with an unspecified number of arguments, no matter how it can be defined. No error can occur here, because of โenormousโ arguments. But, only the first two are counted in the output. You can include the rest of the parameters in the function definition using three dots, like this: โฆ . It must be followed by the array name containing them. The literal meaning of the dots is โgather the remaining parameters into an array.โ Now, letโs cover this example: You have the option of getting the first parameters as variables gathering only the remaining part. In the example below, the first two arguments get into variables, and the rest - into bookTitles array: Please, take into account that the rest parameters should be at the end. As they gather all the remaining arguments, there is no use in them, and they can cause an error: We can distinguish a unique array-like object, called arguments . It includes all arguments by their index. The curious thing about arguments is that although it is both array-like and iterable, it is not an array. The array methods can not work for it. So, arguments.map(...) canโt be called. Moreover, it always comprises the overall arguments. They canโt be captured partially. It would be best if you remembered that arrow functions donโt have "arguments" . If you decide to access the object of arguments from an arrow function, you can get this: The spread syntax allows an array or a string to expand in the places where zero or more arguments or elements are expected. In other words, if you have a string, an array, or an object, and you wish to use all the values, you can spread them into function calls, new objects, or new arrays with a short syntax. Although the spread syntax looks like rest parameters, also using three dots โฆ , it does the opposite. Whenever you use ...arr in the function call, it expands an iterable object arr to the list of arguments. The case of Math.max looks as follows: You can pass different iterables as well, this way: You can integrate the spread syntax with normal values, as follows: It is also desirable to use the spread syntax for merging arrays: You can also use the spread syntax for turning the string into an array of characters. The spread syntax uses iterators for gathering elements the same way as does for..of . The Array.from can also be used for that task, as it converts an iterable into an array, like this: It gives the same result as [...str] . But the subtle difference between the Array.from(obj) and [...obj] is that the first one operates on both iterables and array-likes, but the spread syntax can work only with iterables. As you can notice, the three dots โฆ in the code is either rest parameters or the spread syntax. In this chapter, we represented an easy way of distinguishing between them: Together they allow navigating between an array and a list of parameters smoothly. ุนุฑุจู English Espaรฑol Franรงais Indonesia Italiano ๆฅๆฌ่ช ํ๊ตญ์ด ะ ัััะบะธะน Tรผrkรงe ะฃะบัะฐัะฝััะบะฐ ็ฎไฝไธญๆ function sum ( a , b ) { return a + b ; } alert ( sum ( 1 , 2 , 3 , 4 , 5 ) ) ; function sumAll ( ... args ) { // args is the name for the array let sum = 0 ; for ( let arg of args ) sum += arg ; return sum ; } alert ( sumAll ( 1 ) ) ; // 1 alert ( sumAll ( 1 , 2 ) ) ; // 3 alert ( sumAll ( 1 , 2 , 3 ) ) ; // 6 function showName ( firstName , lastName , ... titles ) { alert ( firstName + ' ' + lastName ) ; // Julius Caesar // the rest go into titles array // i.e. titles = ["Consul", "Imperator"] alert ( titles [ 0 ] ) ; // Consul alert ( titles [ 1 ] ) ; // Imperator alert ( titles . length ) ; // 2 } showName ( "Julius" , "Caesar" , "Consul" , "Imperator" ) ; The rest parameters must be at the end function f ( arg1 , ... rest , arg2 ) { // arg2 after ...rest ?! // error } function showName ( ) { alert ( arguments . length ) ; alert ( arguments [ 0 ] ) ; alert ( arguments [ 1 ] ) ; // it's iterable // for(let arg of arguments) alert(arg); } // shows: 2, Julius, Caesar showName ( "Julius" , "Caesar" ) ; // shows: 1, Ilya, undefined (no second argument) showName ( "Ilya" ) ; Arrow functions do not have "arguments" function f ( ) { let showArg = ( ) => alert ( arguments [ 0 ] ) ; showArg ( ) ; } f ( 1 ) ; // 1 alert ( Math . max ( 3 , 5 , 1 ) ) ; // 5 let arr = [ 3 , 5 , 1 ] ; alert ( Math . max ( arr ) ) ; // NaN let arr = [ 3 , 5 , 1 ] ; alert ( Math . max ( ... arr ) ) ; // 5 (spread turns array into a list of arguments) let arr1 = [ 1 , - 2 , 3 , 4 ] ; let arr2 = [ 8 , 3 , - 8 , 1 ] ; alert ( Math . max ( ... arr1 , ... arr2 ) ) ; // 8 let arr1 = [ 1 , - 2 , 3 , 4 ] ; let arr2 = [ 8 , 3 , - 8 , 1 ] ; alert ( Math . max ( 1 , ... arr1 , 2 , ... arr2 , 25 ) ) ; // 25 let arr = [ 3 , 5 , 1 ] ; let arr2 = [ 8 , 9 , 15 ] ; let merged = [ 0 , ... arr , 2 , ... arr2 ] ; alert ( merged ) ; // 0,3,5,1,2,8,9,15 (0, then arr, then 2, then arr2) let str = "Hello" ; alert ( [ ... str ] ) ; // H,e,l,l,o let str = "Hello" ; // Array.from converts an iterable into an array alert ( Array . from ( str ) ) ; // H,e,l,l,o let arr = [ 1 , 2 , 3 ] ; let arrCopy = [ ... arr ] ; // spread the array into a list of parameters // then put the result into a new array // do the arrays have the same contents? alert ( JSON . stringify ( arr ) === JSON . stringify ( arrCopy ) ) ; // true // are the arrays equal? alert ( arr === arrCopy ) ; // false (not same reference) // modifying our initial array does not modify the copy: arr . push ( 4 ) ; alert ( arr ) ; // 1, 2, 3, 4 alert ( arrCopy ) ; // 1, 2, 3 let obj = { a : 1 , b : 2 , c : 3 } ; let objCopy = { ... obj } ; // spread the object into a list of parameters // then return the result in a new object // do the objects have the same contents? alert ( JSON . stringify ( obj ) === JSON . stringify ( objCopy ) ) ; // true // are the objects equal? alert ( obj === objCopy ) ; // false (not same reference) // modifying our initial object does not modify the copy: obj . d = 4 ; alert ( JSON . stringify ( obj ) ) ; // {"a":1,"b":2,"c":3,"d":4} alert ( JSON . stringify ( objCopy ) ) ; // {"a":1,"b":2,"c":3} If you have suggestions what to improve - please submit a GitHub issue or a pull request instead of commenting. If you can't understand something in the article โ please elaborate. To insert few words of code, use the tag, for more than 10 lines โ use a sandbox ( plnkr , jsbin , codepen โฆ) We want to make this open-source project available for people all around the worl Flash Mature Flashing Forum Non Nude Hot Sensual Amateurs Private Nurse
tag, for several lines โ wrap them in