What are default parameters?
JavaScriptThe short answer
Default parameters let you set a fallback value for function parameters when no argument is passed (or undefined is passed). They were introduced in ES6 and replace the old pattern of checking for undefined inside the function body.
How they work
function greet(name = 'World') { console.log(`Hello, ${name}!`);}greet('John'); // "Hello, John!"greet(); // "Hello, World!"greet(undefined); // "Hello, World!" — undefined triggers the defaultgreet(null); // "Hello, null!" — null does NOT trigger the defaultThe default value is only used when the argument is undefined or not provided. null, 0, '', and false do not trigger the default.
The old way
Before ES6, you had to check manually:
function greet(name) { name = name || 'World'; // breaks if name is "" or 0 // or name = name !== undefined ? name : 'World';}The || approach is buggy because it treats "", 0, and false as missing values. Default parameters solve this correctly.
With objects
A common pattern for options objects:
function createUser({ name = 'Anonymous', role = 'viewer',} = {}) { return { name, role };}createUser({ name: 'John' }); // { name: "John", role: "viewer" }createUser(); // { name: "Anonymous", role: "viewer" }The = {} at the end is important — without it, calling createUser() with no arguments would throw an error because you cannot destructure undefined.
Defaults can use previous parameters
function createElement(tag, className = `${tag}-default`) { return { tag, className };}createElement('div'); // { tag: "div", className: "div-default" }createElement('div', 'custom'); // { tag: "div", className: "custom" }Interview Tip
Show the basic syntax and mention that null does not trigger the default (only undefined does). The destructured options pattern is good to show for bonus points. This is a quick question — keep it concise.
Why interviewers ask this
This tests ES6 knowledge. Interviewers want to see if you know the modern syntax and understand the edge case with null vs undefined.