DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. Easy JavaScript Part 3: What Is a Default Parameter in a Function?

Easy JavaScript Part 3: What Is a Default Parameter in a Function?

A JavaScript developer shows us some of the ins-and-outs the language, focusing on default parameters, and how web devs can use them with their JS functions.

Dhananjay Kumar user avatar by
Dhananjay Kumar
·
Oct. 27, 17 · Tutorial
Like (6)
Save
Tweet
Share
65.53K Views

Join the DZone community and get the full member experience.

Join For Free

A JavaScript function can have default parameter values. Using default function parameters, you can initialize formal parameters with default values. If you do not initialize a parameter with some value, then the default value of the parameter is undefined.

Let us consider the code listed below:

function foo(num1){
    console.log(num1);
}
foo();

While calling the function foo, you are not passing any parameter, so the default value of the variable num1 is set to undefined. However, sometimes you may want to set a default value other than undefined. In the past, the best strategy was to test for the parameter value undefined and then assign a value. So, let us say that in the above example, you want to set the default value of num1 to 9. You can do that as shown in the code listed below:

function foo(num1) {
    if (num1 === undefined) {
        num1 = 9;
    }
    console.log(num1);
}
foo();

ECMAScript 6 introduced default parameters for the function. Using the default parameters features of ECMA 2015, you no longer have to check for an undefined parameter value. Now, you can put 9 as the default value in the parameter itself. You can rewrite the function above to use the default value as shown below:

function foo(num1 =9) {
    console.log(num1);

}
foo();

For the function foo, if num1 parameter's value is not passed, then JavaScript will set 9 as the default value of num1.

Checking for Undefined Parameters

Even if you explicitly pass undefined as the parameter value when calling the function, the parameter value will be set to the default value.

function foo(num1 =9) {
    console.log(num1);
}
foo(undefined);

In the code above, you are passing undefined as the value of num1; therefore, the value of num1 will set to default value 9.

Default Value Evaluated at Run Time

The JavaScript function default value gets evaluated at runtime. To understand this better, consider the code below:

function foo(value = koo()) {
    return value;
}

function koo() {
     return "Ignite UI";
}

var a = foo();
console.log(a);

In the function foo, the default value for the parameter value is set to the function koo. When you call the function foo at runtime, the function koo will be evaluated. Upon calling the foo function, you will get an output like the one shown in the below image (in this example, we're working with the Ignite UI framework).

Reusing Default Parameters

Default parameters are available to be used by later default parameters. Let us consider the code listed below:

function foo(num1 = 9, num2 = num1 + 8){
    console.log(num2);
}
foo();

In the code above, the default value of num1 is used to calculate the default value of num2. You will get the following output when calling the function foo:

Conclusion

The JavaScript default parameter is very useful while writing a function. When calling the function, if a parameter is missing, the default parameter feature allows you to assign a default value to the function parameter, rather than leaving it undefined.

JavaScript

Published at DZone with permission of Dhananjay Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Submit a Post to DZone
  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • How To Handle Secrets in Docker
  • Reliability Is Slowing You Down

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: