
Rest Parameters and arguments object: A Quick JavaScript Guide
When I was learning JavaScript, rest parameters and the arguments object felt confusingly similar. Here's a quick breakdown. Why do we even need it Imagine you're writing a function to calculate the total price of items in a shopping cart. function calculateTotal ( firstItemPrice , secondItemPrice , thirdItemPrice ) { let total = 0 ; total = total + firstItemPrice + secondItemPrice + thirdItemPrice return total ; } calculateTotal ( 10 , 20 , 30 ); // 60 This works perfectly, except for the fact that it works perfectly only for 3 items. What if we need to calculate the price of 5 items? Or of 10 items? We really wouldn't want to pass 10 similar arguments to a function.. We don't even know how many items a user will put in a cart in the first place. And this is when the arguments object and rest parameters come in handy. You need arguments object and rest parameters when you don't know how many parameters will be passed to a function in advance. Arguments Object Usage JavaScript gives us
Continue reading on Dev.to Webdev
Opens in a new tab



