
UX/UI
Take your engagement & conversions to the next level with these UX writing tips!Word your way to a stellar user experience!
April 6, 2023Convert multiple arguments into a single function & simplify your code.
April 6, 2023
By: Enrique
REST is a versatile Javascript and Typescript parameter (a.k.a spread syntax).
It helps you to easily pass an arbitrary number of arguments to a function in the form of an array.
Using REST can help to make code more concise, easier to read, and more robust.
In Node.js, for example, to execute functions successfully you must enter a specific number of parameters.
If any mandatory parameters are missing, the code might break and result in errors that could lead to the site crashing.
By using “… args”, you can pass in an arbitrary number of parameters of any type (strings, objects, numbers) and it will still execute as intended.
function sum(...args) {
let total = 0;
for (const args of args) {
total += args;
}
return total;
}
sum(1, 1); // 2
sum(1, 2, 3, 4, 5); // 15
sum(2, 4, 6, 8, 10, 1, 2, 3, 4, 5); //45
✍🏽This article was written by our copywriter, Sofia, in collaboration with our DEV expert, Enrique.