Default Parameters in JavaScript Functions: From Chaos to Elegance

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

A parameter without a gift? Understanding default values in JavaScript

\n\n

Imagine you ordered a pizza. You call the delivery service and say: "I'd like a pizza, please." The operator asks: "What kind?" — "Well, the standard one, with cheese and sausage." You don't even think about the pizza having default parameters.

\n\n

It's the same in JavaScript: functions are often called without all the arguments. And if the programmer didn't provide a set a default parameter value for a JavaScript function, the code can break or behave unpredictably. In this article, we'll cover all the ways to set a default value for a function parameter in JS — from the old reliable workarounds to modern ES6 syntax. Let's go!

\n\n

Why is this even important?

\n\n

Let's say we have a function that greets a user:

\n\n
function greet(name) {\n    console.log('Hello, ' + name + '!');\n}\n\ngreet(); // Outputs: "Hello, undefined!" — awkward, right?
\n\n

Without default parameters javascript, a missing argument becomes undefined. And if we try to call a method on undefined — we get an error. Therefore, default parameters js is not just a convenience, but a protection against bugs.

\n\n

Method 1: Classic — ternary operator (or if)

\n\n

Before ES6, developers used the ternary operator javascript or a simple check. It works, but looks cumbersome:

\n\n
function multiply(a, b) {\n    a = a !== undefined ? a : 1;\n    b = b !== undefined ? b : 2;\n    return a * b;\n}\n\nconsole.log(multiply(5)); // 10 (5 * 2)\nconsole.log(multiply());  // 2 (1 * 2)
\n\n

Pros: Clear even for beginners, compatible with ancient browsers.
\nCons: Code becomes bloated. If there are 5–6 parameters, readability drops sharply.

\n\n

Hidden pitfall: checking for undefined vs truthy

\n\n

Many beginners write it like this:

\n\n
function greet(name) {\n    name = name || 'Guest';\n    console.log('Hello, ' + name);\n}
\n\n

This works until you pass 0, an empty string '', or false. All these values are falsy, and the function will replace them with 'Guest'. If you need to pass 0 as a valid value — this approach will break. Use a strict check for undefined.

\n\n

Method 2: The magical arguments object

\n\n

Inside every function (except arrow functions), there is an implicit array-like object arguments javascript. It can be used to check the passed arguments:

\n\n
function showInfo(name, age) {\n    name = arguments.length > 0 ? name : 'Anonymous';\n    age = arguments.length > 1 ? age : 'not specified';\n    return `${name}, age: ${age}`;\n}\n\nconsole.log(showInfo()); // Anonymous, age: not specified
\n\n

Pros: Gives full control over which arguments are passed.
\nCons: Syntax is outdated, doesn't work in arrow functions. Usually used only for very specific tasks (e.g., when the number of parameters is unknown in advance).

\n\n

Method 3: Modern standard — ES6 default parameters

\n\n

Starting with ECMAScript 2015, you can set default parameters js directly in the function declaration. This is the cleanest and most recommended way:

\n\n
function multiply(a = 1, b = 2) {\n    return a * b;\n}\n\nconsole.log(multiply(5)); // 10\nconsole.log(multiply());  // 2
\n\n

Beautiful, isn't it? But there are important nuances you need to know to avoid falling into a trap.

\n\n

Nuance 1: Default values are evaluated at call time

\n\n

This means that if you pass undefined — the default will be used. If you pass null — it won't:

\n\n
function test(x = 10) {\n                

Blogs

Book Recommendations