Coding/Learn/javascript/parameters and rest
JavaScript Foundational Course
01.Introduction to JavaScript & V8 Engine
02.Console Output & Program Flow
03.Variables & Data Types (let, const, typeof)
04.Template Literals & String Interpolation
05.Type Coercion & Strict Equality (=== vs ==)
06.Arithmetic, Math Object & Modulo
07.Conditionals & If / Else Branching
08.Logical Operators & Nullish Coalescing (??)
09.Ternary Operator & Switch Statements
10.While & Do-While Loops
11.Standard For Loops & Iteration
12.Loop Control (Break & Continue)
13.Functions, Arrow Syntax & Return Values
14.Default Parameters, Rest & Spread Syntax
15.Arrays, Indexing & Mutating Methods
16.Array Iterators (map, filter, reduce)
17.Objects, Key-Value Pairs & Destructuring
18.Scope, Lexical Environment & Closures
19.Asynchronous JavaScript (Promises & Async/Await)
20.Capstone: Interactive E-Commerce Cart & Ledger Engine
Course Certificate0%

Complete all lessons to earn

LearnDefault Parameters, Rest & Spread Syntax
14 / 20 0%
Lesson 14 of 20
6 mins read

Default Parameters, Rest & Spread Syntax

PreviousNext Lesson
In Plain English

JavaScript parameters allow default fallback values if omitted or passed as undefined. The rest operator (...args) collects multiple arguments into an array, while the spread operator expands arrays or objects into individual elements.

The Mental Model

"Rest parameters are like a laundry bag gathering loose socks into a single bundle (...items). Spread syntax is dumping that bag out flat on a table to inspect each sock individually."

Deep Dive: How It Works

Default Parameters: function greet(name = "Guest") prevents undefined values.

Rest Operator (...args): Must be the last parameter in the function signature and collects remaining arguments into an array.

Spread Operator (...arr): Clones or combines arrays and objects shallowly: const merged = [...a, ...b];.

Syntax Blueprint

function sum(...nums) { ... } | const combined = [...arr1, ...arr2]

Use ... to gather arguments into an array or spread elements across a new structure.

PointCore Rules to Remember

Point star
Default Values: function test(x = 10) sets fallback when x is undefined.
Point star
Rest Parameters: Captures arbitrary number of arguments into a real JavaScript Array.
Point star
Spread Syntax: Expands iterables into lists, function arguments, or new array literals.

Common Beginner Traps & How to Fix Them

Mistake:Placing rest parameters before other named parameters.

Why it happens: function test(...rest, last) is a syntax error because rest must be the final parameter.

How to fix: Always place ...rest as the very last item in the parameter list.

Pro Tip: Use object spread { ...defaults, ...overrides } to cleanly create immutable configuration objects.

Live Interactive Example

Hit Run Code to see it live
Rest Parameters and Spread Merging
JavaScript ES2024
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Output Console
Click "Run Code" to view the rendered output.
How it works: Demonstrates gathering variable arguments and spreading arrays into a merged list.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Merge arrays with spread syntax

  • PointGiven const core = ["CPU", "RAM"]; and const extra = ["GPU", "SSD"];
  • PointCreate const hardware = [...core, ...extra];
  • PointPrint "Hardware: CPU, RAM, GPU, SSD" using hardware.join(", ").
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates