|
1 | 1 | # JavaScript Rest Parameter
|
2 | 2 |
|
3 |
| -Understanding the JavaScript rest parameter |
| 3 | +Understanding the JavaScript Rest Parameter. |
| 4 | + |
| 5 | +## Life without Rest |
| 6 | + |
| 7 | +JavaScript functions don't give a func about extra parameters, they simply ignore them. |
| 8 | +``` |
| 9 | +function add(num1, num2) { |
| 10 | + return num1 + num2 |
| 11 | +} |
| 12 | +
|
| 13 | +console.log(add(100, 200, 500)) // 500 is ignored, lol |
| 14 | +
|
| 15 | +// 300 |
| 16 | +``` |
| 17 | + |
| 18 | +One way to remember the forgotten is to use arguments keyword within a function. |
| 19 | +``` |
| 20 | +function add(num1, num2) { |
| 21 | + return num1 + num2 + arguments[2] |
| 22 | +} |
| 23 | +
|
| 24 | +console.log(add(100, 200, 500)) |
| 25 | +
|
| 26 | +// 800 |
| 27 | +
|
| 28 | +// [Arguments] { '0': 100, '1': 200, '2': 500 } |
| 29 | +``` |
| 30 | + |
| 31 | +But they doesn't jam with arrow arrow functions |
| 32 | +``` |
| 33 | +const add = (num1, num2) => { |
| 34 | + return num1 + num2 + arguments[2] |
| 35 | +} |
| 36 | +
|
| 37 | +console.log(add(100, 200, 500)) |
| 38 | +
|
| 39 | +// 300[object Object] |
| 40 | +
|
| 41 | +// arguments returns root level object |
| 42 | +// (for browser it is window, for Node.js it is module wrapper variables) |
| 43 | +``` |
| 44 | + |
| 45 | +The fall of arguments keyword: |
| 46 | + |
| 47 | +- it is an object acting as an array |
| 48 | +- provide all parameters (given or not given) |
| 49 | +- doesn't jam with arrow arrow functions |
| 50 | +- not an array, so can't use array functions |
| 51 | +- syntactically weirdo |
| 52 | + |
| 53 | +## Rest to the rescue |
| 54 | + |
| 55 | +Put 3 dots before a function's parameter and call it a rest parameter. |
| 56 | +``` |
| 57 | +function add(...numbers) { |
| 58 | + let total = 0 |
| 59 | + for (const num of numbers) { |
| 60 | + total += num |
| 61 | + } |
| 62 | + return total |
| 63 | +} |
| 64 | +
|
| 65 | +console.log(add(100, 200, 500)) |
| 66 | +
|
| 67 | +// 800 |
| 68 | +``` |
| 69 | + |
| 70 | +Leveraging the power of array functions with rest parameter. |
| 71 | +``` |
| 72 | +const add = (...numbers) => numbers.reduce((acc, crr) => acc + crr, 0) |
| 73 | +
|
| 74 | +console.log(add(100, 200, 500)) |
| 75 | +
|
| 76 | +// 800 |
| 77 | +``` |
| 78 | + |
| 79 | +Can also jam with arrow functions. |
| 80 | +``` |
| 81 | +const add = (...numbers) => numbers.reduce((acc, crr) => acc + crr, 0) |
| 82 | +
|
| 83 | +console.log(add(100, 200, 500)) |
| 84 | +
|
| 85 | +// 800 |
| 86 | +``` |
| 87 | + |
| 88 | +Rest must be the last parameter in a function's parameter list. |
| 89 | +``` |
| 90 | +const info = (name, age, ...skills, gender) => `${name} ${age} - ${skills.join(', ')}` |
| 91 | +
|
| 92 | +console.log(info('John', 21, 'Python', 'Bootstrap', 'Sass')) |
| 93 | +
|
| 94 | +// Uncaught SyntaxError: Rest parameter must be last formal parameter |
| 95 | +``` |
| 96 | + |
| 97 | +Failed to do so will leads to error and error nobody wants and dev are not nobody. |
| 98 | +``` |
| 99 | +const info = (name, age, ...skills, gender) => `${name} ${age} - ${skills.join(', ')}` |
| 100 | +
|
| 101 | +console.log(info('John', 21, 'Python', 'Bootstrap', 'Sass')) |
| 102 | +
|
| 103 | +// Uncaught SyntaxError: Rest parameter must be last formal parameter |
| 104 | +``` |
| 105 | + |
| 106 | +Summary about rest parameter: |
| 107 | + |
| 108 | +- its simply an array |
| 109 | +- benefited with array functions |
| 110 | +- jam with arrow functions |
| 111 | +- must be the last parameter |
| 112 | +- syntactically awesome |
0 commit comments