DEV Community

Cover image for JavaScript Rest Parameters: Because Sometimes You Just Need More Args
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on • Edited on

JavaScript Rest Parameters: Because Sometimes You Just Need More Args

Hello, I'm Maneshwar. I'm building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.

Hello, I'm Maneshwar. I'm working on FreeDevTools online currently building **one place for all dev tools, cheat codes, and TLDRs* — a free, open-source hub where developers can quickly find and use tools without any hassle of searching all over the internet.

Ever found yourself writing a function and thinking, “I don’t know how many arguments this will need... maybe 2, maybe 20?” Well, JavaScript has your back with something called rest parameters.

They're like a magic bag that scoops up all the leftover arguments into a neat array.

Let’s dive into what they are, how to use them, and why they’re awesome.

No pizza arguments here—just code
.

The Basics

Rest parameters use the ... syntax to gather extra arguments passed to a function into an array.

function sum(...theArgs) {
  let total = 0;
  for (const arg of theArgs) {
    total += arg;
  }
  return total;
}

console.log(sum(1, 2, 3));    // 6
console.log(sum(1, 2, 3, 4)); // 10
Enter fullscreen mode Exit fullscreen mode

You can pass in as many arguments as you like, and the function just gobbles them all up.

Syntax Rules (AKA the Fine Print)

  • Only one rest parameter is allowed.
  • It must be the last parameter.
  • No default values or trailing commas allowed after it.

This is valid:

function logThings(a, b, ...others) {
  console.log(others);
}
Enter fullscreen mode Exit fullscreen mode

This will scream at you:

function wrong1(...one, ...two) {}     
function wrong2(...rest, next) {}      
function wrong3(...stuff,) {}          
function wrong4(...stuff = []) {}      
Enter fullscreen mode Exit fullscreen mode

How It Works

Let’s look at a simple example:

function myFun(a, b, ...manyMoreArgs) {
  console.log("a", a);
  console.log("b", b);
  console.log("manyMoreArgs", manyMoreArgs);
}

myFun("one", "two", "three", "four", "five");
Enter fullscreen mode Exit fullscreen mode

Console output:

a one
b two
manyMoreArgs [ 'three', 'four', 'five' ]
Enter fullscreen mode Exit fullscreen mode

Even if you don’t pass extra values, manyMoreArgs is still a real array:

myFun("one", "two"); 
// manyMoreArgs -> []
Enter fullscreen mode Exit fullscreen mode

Only pass one extra?

myFun("one", "two", "three"); 
// manyMoreArgs -> [ "three" ]
Enter fullscreen mode Exit fullscreen mode

Counting Arguments with Rest

Since rest parameters are just arrays, you can easily use .length:

function fun1(...theArgs) {
  console.log(theArgs.length);
}

fun1();         
fun1(42);       
fun1(1, 2, 3);  
Enter fullscreen mode Exit fullscreen mode

Combine with Regular Params

You can mix normal parameters with rest ones. Example:

function multiply(multiplier, ...theArgs) {
  return theArgs.map((num) => multiplier * num);
}

console.log(multiply(2, 10, 20, 30)); 
Enter fullscreen mode Exit fullscreen mode

Rest vs arguments (Aka The Old Way)

You could use the arguments object… but it's kinda stuck in the past.

function oldWay() {
  const args = Array.from(arguments);
  console.log(args.sort());
}
Enter fullscreen mode Exit fullscreen mode

But arguments is not a real array, so this won’t work:

function nope() {
  arguments.sort();  
}
Enter fullscreen mode Exit fullscreen mode

With rest parameters, you’re already working with a real array:

function sortArgs(...args) {
  return args.sort();
}

console.log(sortArgs(5, 3, 7, 1)); 
Enter fullscreen mode Exit fullscreen mode

Destructuring Rest Params

You can even destructure them, skipping the first few values:

function ignoreFirst(...[, b, c]) {
  return b + c;
}

console.log(ignoreFirst(10, 20, 30)); // 50
Enter fullscreen mode Exit fullscreen mode

TL;DR

✅ Use ...rest when you don’t know how many arguments you’ll get

✅ It's a clean, modern alternative to the arguments object

✅ Works great with array methods like map, sort, forEach

❌ Only one rest param per function

❌ It must come last


A collection of UI/UX-focused tools crafted to simplify workflows, save time, and reduce friction in searching tools/materials.

Any feedback or contributors are welcome!

It’s online, open-source, and ready for anyone to use.

Let’s make it even better together.

git-lrc
*AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.*

Any feedback or contributors are welcome! It's online, source-available, and ready for anyone to use.

⭐ Star it on GitHub:

GitHub logo HexmosTech / git-lrc

Free, Unlimited AI Code Reviews That Run on Commit

git-lrc logo

git-lrc

Free, Unlimited AI Code Reviews That Run on Commit


git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

AI agents write code fast. They also silently remove logic, change behavior, and introduce bugs -- without telling you. You often find out in production.

git-lrc fixes this. It hooks into git commit and reviews every diff before it lands. 60-second setup. Completely free.

See It In Action

See git-lrc catch serious security issues such as leaked credentials, expensive cloud operations, and sensitive material in log statements

git-lrc-intro-60s.mp4

Why

  • 🤖 AI agents silently break things. Code removed. Logic changed. Edge cases gone. You won't notice until production.
  • 🔍 Catch it before it ships. AI-powered inline comments show you exactly what changed and what looks wrong.
  • 🔁 Build a habit, ship better code. Regular review → fewer bugs → more robust code → better results in your team.
  • 🔗 Why git? Git is universal. Every editor, every IDE, every AI…




Top comments (4)

Collapse
 
nevodavid profile image
Nevo David

i think stuff like rest parameters is actually super helpful for me - makes me curious, you think the old way of grabbing function args ever made sense or was it always just awkward?

Collapse
 
ngam_vitalisyuh_3161eef2 profile image
Ngam Vitalis Yuh

Great insights, thank you for the knowledge

Collapse
 
lurah profile image
Adrie T

Great to know this. Is there something like **kwargs also in JavaScript?

Collapse
 
danyk2099 profile image
Daniel

Looks good, but why not just use an array as a param?