๐๐๐ฒ ๐ of 100DaysOfCode
- ๐๐๐จ๐ฉ ๐๐๐ง๐๐ข๐๐ฉ๐๐ง๐จ -
Before knowing what rest parameters are, I would like to talk first about the โargumentsโ object in JS.
The โargumentsโ object is an ๐ข๐ณ๐ณ๐ข๐บ-๐ญ๐ช๐ฌ๐ฆ object that is found as a local variable within all ๐ฏ๐ฐ๐ฏ-๐ข๐ณ๐ณ๐ฐ๐ธ functions. You can refer to a functionโs arguments inside that function by using its โargumentsโ object.
I said it is an array-like object; since, like arrays, they have entries for each argument the function was called with, with the first entryโs index at 0(ex. arguments[0]), and the function length is also included within. However, they donโt have other array functions like forEach and map functions.
The โargumentsโ object can access any argument of a function even when not specified in the declaration of the said function, which means that the function would have access for infinite arguments.
Now back to rest parameters ![]()
much like โargumentsโ, they allow the function to have access to indefinite number of arguments as an ๐๐๐๐๐.
A function definitionโs last parameter can be prefixed with " โฆ " which will cause all remaining (user supplied) parameters to be placed within an Array object.
Rest Parameters are not limited to only the length function, they can perform what any array can do like using slice, forEach, map, sort, etcโฆ
As they are used as the last parameter, it leaves space for other parameters to be specified.
