When we see “…” operator in the code, it is either rest parameters or the spread syntax.
There’s an one of easy way to distinguish between them:
⦁ When … operator is at the end of function parameters, it’s “rest parameters” and gathers the rest of the list of arguments into an array.
⦁ When … operator occurs in a function call or alike, it’s called a “spread syntax” and expands an array into a list.
Use patterns:
Rest parameters are used to create javascript functions that accept any number of arguments.
The spread(…) syntax is used to pass an array to javascript functions that normally require a list of many arguments.
Together they help to travel between a list and an array of parameters([…arr])with ease.
javascript skip default parameter:
function myfunc(x,y=2,z=6){ console.log(x*y); console.log(z); } myfunc(5) //Output: 10 6 myfunc(5,4,2) //Output: 20 2 myfunc(5,undefined,17) //Output 10 17
skip arguments in js:
function userInfo(firstName, lastName, userName){ // other logic stuff console.log(firstName, lastName, userName); } // skip lastName argument using: ...[,] and it's mean undefine // 1 argument for ...[,] 2 arguments for ...[,,] and so on..... userInfo('JavaScript', ...[,], 'js'); //it's mean => firstName: JavaScript, lastName: undefined, userName: js