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.