How to have optional parameters with default values

Discussion in 'Javascript' started by wyo, Apr 27, 2007.

  1. wyo

    wyo Guest

    I have a function with several parameter, each optional.

    function test (a, b, c, d) {...}

    How do I check if e.g. parameter "a" is missing and set it a default
    value?
    How can I use it with parameter "a" optional but "b", "c", "d"
    specified?

    O. Wyss
     
    wyo, Apr 27, 2007
    #1
    1. Advertising

  2. On Apr 27, 7:12 am, wyo <> wrote:
    > I have a function with several parameter, each optional.
    >
    > function test (a, b, c, d) {...}
    >
    > How do I check if e.g. parameter "a" is missing and set it a default
    > value?


    if(a===undefined)a='default value'

    > How can I use it with parameter "a" optional but "b", "c", "d"
    > specified?


    if(b===undefined)doSomething()
     
    scripts.contact, Apr 27, 2007
    #2
    1. Advertising

  3. wyo

    wyo Guest

    On Apr 27, 3:35 pm, "scripts.contact" <>
    wrote:
    > On Apr 27, 7:12 am, wyo <> wrote:
    >
    >
    > > How can I use it with parameter "a" optional but "b", "c", "d"
    > > specified?

    >
    > if(b===undefined)doSomething()


    and calling it as

    test (,value_b, value_c, value_d);

    Just leave off parameter "a"?

    O. Wyss
     
    wyo, Apr 27, 2007
    #3
  4. wyo

    RobG Guest

    wyo wrote:
    > I have a function with several parameter, each optional.
    >
    > function test (a, b, c, d) {...}
    >
    > How do I check if e.g. parameter "a" is missing and set it a default
    > value?


    The first parameter passed to the function will be assigned to a, the
    second to b, etc. If a is missing, then so are b, c and d.

    function test (a, b, c, d, a) {
    a = a || 'a';
    b = b || 'b';
    ...
    }


    > How can I use it with parameter "a" optional but "b", "c", "d"
    > specified?


    function test (b, c, d, a) {...}

    Parameters are passed by order, not name.


    --
    Rob
     
    RobG, Apr 27, 2007
    #4
  5. wyo

    wyo Guest

    On 27 Apr., 16:23, RobG <> wrote:
    > Parameters are passed by order, not name.
    >

    That means I've to give a dummy value like -1 for the first
    parameter.

    Thanks
    O. Wyss
     
    wyo, Apr 27, 2007
    #5
  6. wyo wrote:
    > On 27 Apr., 16:23, RobG <> wrote:
    > > Parameters are passed by order, not name.
    > >

    > That means I've to give a dummy value like -1 for the first
    > parameter.


    yes e.g. 0, empty string, null, undefined etc..
     
    scripts.contact, Apr 28, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Do
    Replies:
    1
    Views:
    67,534
    Kevin Spencer
    Oct 16, 2003
  2. ruca

    Optional and have default value

    ruca, Apr 12, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    435
    Marina
    Apr 12, 2004
  3. Hanover

    SQLCommand with Optional Parameters?

    Hanover, Sep 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    1,494
    rviray
    Sep 28, 2005
  4. Marc Aymerich
    Replies:
    6
    Views:
    253
    Corey Richardson
    Jun 26, 2011
  5. Surgeon
    Replies:
    4
    Views:
    141
    Ross Bamford
    Dec 31, 2005
Loading...

Share This Page