Bits Kingdom logo with a hexagon lattice, uppercase text in white, and a minimalistic design.

Get ready to REST! Simplify Your Code with Ease

Use rest parameters to streamline your JavaScript and TypeScript functions.

by Apr 6, 2023Development

Home / Development / Get ready to REST! Simplify Your Code with Ease

Working with functions that accept a long list of arguments? JavaScript (and TypeScript) gives us a clean solution: rest parameters. They let you handle multiple inputs more efficiently and make your code more flexible, readable, and easier to maintain.

What Is rest in JavaScript?

In JavaScript, rest parameters allow you to collect multiple function arguments into a single array. The syntax is simple: three dots (...) before a named parameter in a function definition.

Example:

function logAll(...values) {
  console.log(values);
}

logAll(1, 2, 3); // [1, 2, 3

Here, …values is a rest parameter. It gathers any number of arguments passed into logAll() and places them in the values array.

Why is it called “rest”?

The name comes from the fact that the ... syntax captures “the rest” of the arguments that haven’t been matched by earlier named parameters. It was introduced in ECMAScript 2015 (ES6)—the same major update that brought us features like arrow functions, let/const, and template literals.

Don’t Confuse It with Spread

Though they use the same ... syntax, rest and spread do opposite things:

  • Rest collects multiple values into one array (usually in function parameters).
  • Spread expands an array (or object) into individual values.
// Rest
function greet(greeting, ...names) {
  return `${greeting} ${names.join(", ")}`;
}
greet("Hello", "Alice", "Bob"); // Hello Alice, Bob

// Spread
const numbers = [1, 2, 3];
Math.max(...numbers); // 3

Why Use Rest Parameters?

Here’s why rest parameters are such a helpful addition to your dev toolkit:

  • Cleaner function signatures – No need to manually handle undefined or missing arguments.
  • More flexibility – Perfect for functions that need to handle a dynamic number of inputs.
  • Fewer bugs – You’re less likely to break your code when argument length changes.

Especially in Node.js, where function parameters vary frequently (think: middleware, route handlers, or event listeners), rest parameters help you write more robust and concise code.

Real-World Example: Sum Function

Let’s say you need a function to add any number of numbers. Before ES6, you’d have to mess with arguments or manually validate inputs. Now, it’s cleaner and more readable:

function sum(...args) {
  let total = 0;
  for (const arg of args) {
    total += arg;
  }
  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

🛑 Not to Be Confused With: REST APIs

It’s easy to mix up rest parameters with REST in web development—short for REpresentational State Transfer, a style for designing web APIs.

  • Rest parameters = JavaScript syntax for handling variable arguments
  • REST API = A web architecture style for communicating between systems

Final Thoughts

Whether you’re building utility functions or dealing with unpredictable user input, rest parameters help you write JavaScript and TypeScript that’s simpler, cleaner, and easier to maintain.

Start using them where it makes sense—and keep your code flexible and future-ready.

Want more practical dev tips? Check out our article on how to master the browser console—a must-read for developers who want to debug like pros.

Explore more topics:

Inner Light Ministries: Empowering Spiritual Growth Online

Creating a Digital Sanctuary for Spiritual Awakening