[JavaScript] Understanding Default Parameter - Explained with Examples.

Lim WoojaeLim Woojae
1 min read

Introduction

This article introduces a default parameter.

The default parameter basically prevents a situation where users enter nothing in the parameter when they are supposed to.


Default Parameter

Syntax:

function myFunction(parameter1 = value) {
    console.log(value);
}

myFUnction(); // value

Example:

let defaultUserName = "strongjohn";
function myFunction(userName = defaultUserName) {
    console.log("Hello,", userName);
}

myFunction(); // Hello, strongjohn
myFunction("strawberry lover"); // Hello, strawberry lover

Do you notice that defaultUserName variable is printed even though the function received no parameter?

You can make your program better if you can use the default parameter correctly.

0
Subscribe to my newsletter

Read articles from Lim Woojae directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Lim Woojae
Lim Woojae

Computer Science Enthusiast with a Drive for Excellence | Data Science | Web Development | Passionate About Tech & Innovation