Variable arguments

3 snippets across 2 stacks - JavaScript, Python

JSJavaScript

Rest Parameters

JS · Variables & Constants
Syntax
function fn(...args) {}
Example
function sum(...numbers) {
  return numbers.reduce((total, n) => total + n, 0);
}
console.log(sum(5, 10, 15));
Output
30

Note Rest must be the last parameter. Unlike the old arguments object, rest gives you a real Array with all array methods.

Rest Parameters in Functions

JS · Functions
Syntax
function fn(first, ...rest) {}
Example
function logTagged(level, ...messages) {
  const timestamp = new Date().toISOString();
  console.log(`[${level}] ${timestamp}:`, ...messages);
}
logTagged("INFO", "Server started", "on port 3000");
Output
[INFO] 2026-04-04T...: Server started on port 3000

Note Rest parameters collect remaining arguments into a real Array. Unlike arguments, they work in arrow functions too.

PYPython

*args and **kwargs

PY · Functions
Syntax
def func(*args, **kwargs):
Example
def log_event(event: str, *tags: str, **metadata: str):
    print(f"Event: {event}")
    print(f"Tags: {tags}")
    print(f"Meta: {metadata}")

log_event("login", "auth", "user", ip="10.0.0.1")
Output
Event: login
Tags: ('auth', 'user')
Meta: {'ip': '10.0.0.1'}

Note *args collects extra positional arguments as a tuple, **kwargs collects extra keyword arguments as a dict. They can coexist in the same signature.

Frequently asked questions

How does JavaScript handle variable arguments?
This task is covered in 2 stacks on this page: JavaScript, Python. The "Rest Parameters" snippet in JavaScript uses `function fn(...args) {}`.
Which code does the JavaScript example use?
The "Rest Parameters" snippet uses `function fn(...args) {}`, from the Variables & Constants section of the JavaScript cheat sheet.
Which stacks cover "variable arguments" on this page?
JavaScript, Python. Together they hold 3 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Rest Parameters": Rest must be the last parameter. Unlike the old arguments object, rest gives you a real Array with all array methods.