• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • JavaScript

Default Parameter in JavaScript function

  • Yatendrasinh Joddha
  • March 17, 2018
  • 2 minute read
javascript
Total
0
Shares
0
0
0

In any programming language we often require having default parameter or default value for the parameter in a function. JavaScript allow us to initialize parameter a default value. If you are not passing any value to the parameter, then the default value of the parameter will be undefined.

Let’s consider below code:

function addition(num1, num2) {
  return num1 + num2;
}
addition();

Here, you are calling function “addition()” in which you are not passing any parameter. So, the default value for the parameters “num1” and “num2” will be set to undefined.

Now in past if you want to set a default value to the “num1” and “num2” you have to do it like:

function addition(num1, num2) {
     if(num1 === undefined){
          num1 = 10;
     }
     if(num2 === undefined){
          num2 = 20;
     }
     return num1 + num2;
}
addition();

In ECMA 2015 features ECMAScript 6 has introduced a default parameter feature, in which it allows us to set default value to the parameter of a function. So for the function “addition()” if you want to assign a parameters with default values, you have to write below code:

function addition(num1 = 10, num2 = 20) {
     return num1 + num2;
}
addition();

In above code while calling function “addition()” we are not passing any parameter. So, as we have assigned default values to the parameters, “num1” will be assigned value 10 and “num2” will be assigned a value 20 so the function will return the answer “30”.

Experiment 1: Passing an Undefined

function addition(num1 = 10, num2 = 20) { 
  return num1 + num2; 
} 
addition(undefined, undefined);

In above code we are passing “undefined” for both arguments of the function, so the function again will return the answer “30”.

Experiment 2: Passing an Parameter in Parameter

function addition(num1 = 10, num2 = num1 + 10) { 
  return num1 + num2; 
} 
addition();

In above code we are passing “num1” as a value to the “num2”. This will work fine, and the function will return an answer “30”.

Experiment 3: Passing a function in Parameter

function addition(num1 = 10, num2 = getValue()) { 
  return num1 + num2; 
} 

function getValue() {
  return 20;
}
addition();

In above code we are calling a function “getValue()” to assign a default value to the parameter “num2”. When we will call function “addition()”, at run time function “getValue()” will be called and value 20 will be assigned to the parameter “num2”. So, the function will return an answer 30.

Interested in JavaScript? Click here to read more JavaScript articles in Code4Developers

Yatendrasinh Joddha
Yatendrasinh Joddha

I have healthy knowledge and experience in Azure, O365 and ASP.Net with c#, MVC, WebApi along with client-side frameworks and libraries like JavaScript, JQuery, KnockoutJs, AngularJs, Angular, ReactJs, NodeJs

Views: 10,420

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0
3 comments
  1. Abdellatif Derbel says:
    March 18, 2018 at 4:00 pm

    The problem is the js default param not working on Safari

    Reply
    1. Yatendrasinh Joddha says:
      March 18, 2018 at 6:00 pm

      Yes, as default parameter is ECMASCRIPT 2015 feature many browser does not support it yet. Safari is one of them

      Reply
  2. Anonymous says:
    March 28, 2018 at 4:48 pm

    Experiment 3: Passing a function in Parameter is really an interesting one

    Reply

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    1 year ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
  • New Learning Models for Fall 2020

    New Learning Models for Fall 2020

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

%d