Sitemap
JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.

Member-only story

JavaScript Daily Tip

4 min readNov 7, 2024

--

Press enter or click to view image in full size

Hello, JavaScript fans! ☀️

Today, let’s dive into a powerful feature in JavaScript that can simplify working with functions — Rest Parameters. Rest parameters provide a flexible way to handle function arguments, making it easier to work with multiple values and dynamic inputs. If you’re not familiar with how they work, this article will show you how to use rest parameters like a pro!

Not a Member? Read for FREE here.

What are Rest Parameters?

In JavaScript, the rest parameter syntax (`…`) allows a function to accept an indefinite number of arguments as an array. This is incredibly useful for functions where you don’t know the exact number of arguments you’ll receive. Rest parameters are created by adding three dots (`…`) before the last parameter in a function definition.

Example:

function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}

console.log(sum(1, 2, 3)); // Output: 6
console.log(sum(4, 5, 6, 7, 8)); // Output: 30

In this example, `sum` can take any number of arguments, thanks to the rest parameter, and `numbers` is treated as an array containing all the arguments passed to `sum`.

--

--

Dipak Ahirav
Dipak Ahirav

Written by Dipak Ahirav

AI & Full-Stack Engineer | Technical Writer. Sharing practical insights on Angular, JavaScript, AI systems, and scalable software architecture.

JavaScript in Plain English
JavaScript in Plain English

Published in JavaScript in Plain English

New JavaScript and Web Development content every day. Follow to join our 3.5M+ monthly readers.