Default function arguments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    Default function arguments

    Is it possible to set default arguments for functions like you are able to in PHP?

    Like so:

    [code=javascript]
    function(type = 'default', default = 'argument')
    // ...
    [/code]

    ?

    Cheers guys
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    not like that ... but you may use:

    [CODE=javascript]function my_func(arg0) {
    // if arg0 is not passed to my_func
    if (typeof arg0 == 'undefined') {
    arg0 = 'default_val0';
    }

    // now use arg0 and further code
    }[/CODE]
    kind regards

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Originally posted by gits
      hi ...

      not like that ... but you may use:

      [CODE=javascript]function my_func(arg0) {
      // if arg0 is not passed to my_func
      if (typeof arg0 == 'undefined') {
      arg0 = 'default_val0';
      }

      // now use arg0 and further code
      }[/CODE]
      kind regards
      Yeh, thought that might be the case.
      Was just looking for a prettier alternative ^_^

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        This should hopefully be supported in the next major version of JavaScript/ECMAScript or something similar. In the meantime, you could use something like this.

        Comment

        • rnd me
          Recognized Expert Contributor
          • Jun 2007
          • 427

          #5
          Code:
          function shout(message){
               alert( message || "hello world" )
          }

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            That's effective in most cases,but if you pass null, false, 0, empty string, etc. it will return "hello world" which is probably not what you want.

            Comment

            • rnd me
              Recognized Expert Contributor
              • Jun 2007
              • 427

              #7
              Originally posted by acoder
              That's effective in most cases,but if you pass null, false, 0, empty string, etc. it will return "hello world" which is probably not what you want.
              thats a good point. any falsy -evaluating variable would trigger the default.

              if you want the defaults only upon undefineds, try something like:

              Code:
                function shout(message){
                  alert( message != undefined ? message : "hello world" )
                }

              Comment

              • mrhoo
                Contributor
                • Jun 2006
                • 428

                #8
                Code:
                function (message){
                         message= (message !== undefined)? message : "default" ;
                }
                !== not equal and same type

                Comment

                Working...