QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads Default Function Arguments Values in JavaScript

Default Function Arguments Values in JavaScript

This post is a part 4 of ECMAScript 6 Complete Tutorial post series.

ES6 provides a new syntax that can be used to define default values to function parameters:

function myFunction(x = 1, y = 2, z = 3)
{
     console.log(x, y, z); // Output "6 7 3"
}
myFunction(6,7);

Also, passing undefined is considered as missing an argument. Here is an example
to demonstrate this:

   function myFunction(x = 1, y = 2, z = 3)
   {
     console.log(x, y, z); // Output "1 7 9"
   }
   myFunction(undefined,7,9);

Defaults can also be expressions. Here is an example to demonstrate this:

   function myFunction(x = 1, y = 2, z = 3 + 5)
   {
     console.log(x, y, z); // Output "6 7 8"
   }
   myFunction(6,7);
Feb 13, 2015Narayan Prusty
JavaScript Function Return Multiple ValuesJavaScript "..." Operator
Comments: 7
  1. Putang Ina
    6 years ago

    That is so easy. Give us harder example. You said that you are founder of QScutter and QNimate. Why your website is so lame ?

    ReplyCancel
  2. pachaiappan
    6 years ago

    pls difference between ECMA 6 and Reactnative, then how its working?.

    ReplyCancel
  3. Arnoud
    6 years ago

    this is also nice:

    function myFunction(x = 1, y = 2, z = 3 + y)
    {
    console.log(x, y, z); // Output "6 7 10"
    }
    myFunction(6,7);

    ReplyCancel
  4. Juliet
    6 years ago

    Great site…

    ReplyCancel
  5. Michu
    7 years ago

    What is the = {} piece for in this?

    function function_name({x = 12, y = 13} = {})

    It works for me without it.

    ReplyCancel
    • Michu
      7 years ago

      Plus, it actually works without curlies too, just:
      function function_name(x = 12, y = 13)
      Am I missing something?

      ReplyCancel
      • Michu
        7 years ago

        OK so after short research I think I know the point. So curly braces in arguments list opens function for named parameters (passing them in object syntax) instead of passing them one by one. But = {} is a mistake I believe.

        ReplyCancel

Leave a Reply Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

8 years ago 7 Comments Web Development
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
Related Articles
  • JavaScript “yield” Keyword and “function*()” Syntax
  • JavaScript Function Return Multiple Values
  • JavaScript “…” Operator
  • JavaScript “for of” Loop Tutorial
  • Javascript “use strict” Tutorial with Examples
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license