JavaScript Rest Parameters

Beginner
6 min read
Updated: Nov 2026
3 examples

Definition and Usage

Rest parameters use ... in a function’s parameter list to collect the remaining arguments into a real array.

JavaScript Syntax

JavaScript
function sum(...nums) {
  return nums.reduce((total, n) => total + n, 0);
}

console.log(sum(1, 2, 3));
console.log(sum(10, 5));
Try it Editor

How It Works

  1. Gather. Every argument lands in the nums array.
  2. Real array. You can call array methods like reduce directly.
  3. Any count. Callers may pass zero or more values.

Examples

Example 1 — Head and Tail

Name the first argument, rest gets the remainder.

JavaScript
function describe(first, ...rest) {
  return first + " +" + rest.length + " more";
}
console.log(describe("a", "b", "c"));
Try it Editor

How It Works

  1. First named. first is "a".
  2. Rest array. rest is ["b", "c"].

Example 2 — Max of Many

Forward a rest array into Math.max.

JavaScript
function maxOf(...nums) {
  return Math.max(...nums);
}
console.log(maxOf(3, 9, 2));
Try it Editor

How It Works

  1. Collect. Rest builds an array of numbers.
  2. Spread out. Spread sends them back as separate arguments.

Example 3 — Rest vs arguments

Rest is a true array; arguments is array-like.

JavaScript
function show(...rest) {
  console.log(Array.isArray(rest));
  console.log(rest.map((n) => n * 2).join(", "));
}
show(1, 2, 3);
Try it Editor

How It Works

  1. Array.isArray. Rest parameters are real arrays.
  2. map works. No need to convert with Array.from.

Frequently Asked Questions

A ...name parameter that packs leftover arguments into an array.

Rest gathers (in parameters or destructuring). Spread expands an array into arguments or elements.

It must be the last parameter.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful