How to have optional parameters with default values

W

wyo

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
 
S

scripts.contact

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()
 
R

RobG

wyo said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top