JavaScript ES2023 ·
✓ verified by execution on 2026-08-11
Functions in JavaScript are incredibly flexible. You don’t always have to pass the exact number of arguments that a function expects. Sometimes you want optional parameters with fallback values, and other times you want to accept an indefinite number of arguments.
JavaScript provides two powerful features for this: default parameters and rest parameters.
Default Parameters
Default function parameters allow named parameters to be initialized with default values if no value or undefined is passed.
javascript
function multiply(a, b = 1) { return a * b;}console.log(multiply(5, 2));console.log(multiply(5));
Output
10
5
Your output
When we omit the second argument, b takes the default value of 1.
Undefined vs Null
A common trap is assuming that any falsy value triggers a default parameter. This is not true! Only undefined triggers default parameters. Falsy values like false, 0, or null do not.
javascript
function greet(name = "Guest") { return name;}console.log(greet(undefined));console.log(greet(null));
Output
Guest
null
Your output
As you can see, passing null explicitly sets the value to null, overriding the default.
Referencing Previous Parameters
One unique feature of JavaScript’s default parameters is that parameters defined earlier (to the left) are available to later default parameters.
javascript
function greet(name, greeting = name) { return greeting;}console.log(greet("Alice"));
Output
Alice
Your output
In this example, the greeting parameter defaults to the value of name if not explicitly provided!
Rest Parameters
The rest parameter syntax allows a function to accept an indefinite number of arguments as an array. This is extremely useful when you want to write a function that can take any number of inputs.
javascript
function sum(...args) { return args.reduce((a, b) => a + b, 0);}console.log(sum(1, 2, 3));
Output
6
Your output
By prefixing the parameter with three dots (...), JavaScript bundles all remaining arguments into a true array. Unlike the old arguments object, rest parameters are real arrays, which means you can use array methods like map, filter, and reduce.
Ordering Rules
There is one strict rule: the rest parameter must be the last parameter in the function definition.
Predict the outputjavascript
Predict what happens if you place a parameter AFTER a rest parameter:
try { new Function("function f(a, ...theArgs, b) { return theArgs; }");} catch (e) { console.log(e.name + ": " + e.message);}
Output
SyntaxError: Rest parameter must be last formal parameter
You predicted
Because it’s a syntax error, the code won’t even execute! You can only have one rest parameter, and it must sit at the very end of your parameter list.
Here, multiplier captures the first argument (2), and theArgs collects the rest ([1, 2, 3]).
Empty Rest Parameters
If there are no additional arguments, the rest parameter will be an empty array.
javascript · visualize
function getRest(...args) { return args.length;}console.log(getRest());
Step through the visualizer above to see how args is initialized as an empty array [] rather than undefined when no arguments are passed.
Visual Summary
Here is a conceptual look at how regular parameters, default parameters, and rest parameters fit together in a function signature:
Function Parameter Types
Check yourself
What happens if you pass null to a parameter that has a default value?
Reveal answer
The parameter evaluates to null. — Only undefined triggers default parameters. Falsy values like false, 0, or null do not.
Where must a rest parameter be placed in a function's parameter list?
Reveal answer
It must be the last parameter. — The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, but it must be the last parameter in the function definition.
What is the difference between the rest parameter and the arguments object?
Reveal answer
Rest parameters are real arrays, while arguments is not. — Rest parameters are true arrays, giving you access to all array methods like map, filter, and reduce, whereas the arguments object is array-like but not an actual array.
Challenges
🐞 Bug Hunt+50 XP
Fix the syntax error caused by rest parameter position.
This code runs. It just does the wrong thing. Read it, find the defect,
fix it — the tests below decide when you are right.
javascript
Test 1 — expects "[3]\n"
Need a hint? (−25% XP)
The rest parameter must be last.
Show solution (0 XP)
function f(a, b, ...theArgs) { return theArgs;}console.log(JSON.stringify(f(1, 2, 3)));
Challenge 2 +50 XP
Fix the default parameter.
javascript
Test 1 — expects "1\n"
Need a hint? (−25% XP)
Undefined triggers defaults.
Show solution (0 XP)
function f(a = 1) { return a; }console.log(f(undefined));
Challenge 3 +50 XP
Use rest parameters to sum.
javascript
Test 1 — expects "3\n"
Need a hint? (−25% XP)
...
Show solution (0 XP)
function sum(...args) { return args.reduce((a,b)=>a+b,0); }console.log(sum(1,2));
Challenge 4 +50 XP
Order
javascript
Test 1 — expects "undefined\n"
Need a hint? (−25% XP)
...
Show solution (0 XP)
function fn(a, b=2) { return a; }console.log(fn());
Challenge 5 +50 XP
Rest is an array
javascript
Test 1 — expects "true\n"
Need a hint? (−25% XP)
...
Show solution (0 XP)
function r(...a) { return Array.isArray(a); }console.log(r());
Challenge 6 +50 XP
Default expr
javascript
Test 1 — expects "number\n"
Need a hint? (−25% XP)
...
Show solution (0 XP)
function f(a=Math.random()) { return typeof a; }console.log(f());