Ad space (banner)
🟨JavaScript Lessons
Lesson 27 / 69

Default Arguments (Setting Initial Values for Parameters)

In this lesson you'll learn how to write default arguments in JavaScript so you can set an initial value that's used when an argument is omitted. This is for people searching "JavaScript default arguments usage" or "JavaScript parameter default value."

Writing = value after a function parameter sets an "initial value" that's automatically used when the caller omits that argument. Picture it like: "if the order form is left blank, you get the usual default set." This saves you from writing code that checks and sets a fallback value inside the function every single time, letting you write a simple, readable function definition.

The sample code defines a function greet(name = "Guest"): calling it as greet("Alice") uses that value, while calling it as greet() with the argument omitted uses the default value "Guest". Without default arguments, you'd have to write something like if (name === undefined) { name = "Guest"; } inside the function every time.

A common beginner stumbling block is that a default argument only applies "when the argument wasn't passed at all, or was passed as undefined." If you pass null, the default value is *not* used — it stays null. Also, when a function has multiple parameters, it's considered more readable to place parameters with default values after the others.

In real development, default arguments are commonly used when designing a function where only part of the configuration can be changed, or a function with optional features. Since it keeps the caller's code simple, it's an indispensable technique for designing an API that's pleasant to use.

Arrow functions can have default arguments written the same way, in a form like (name = "Guest") => { ... }. Even as the way you write functions changes, the underlying idea stays the same, so it's worth remembering alongside the rest.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)