Sunday, 16 July 2017

javascript - pre-defined parameters




I want to be able to do this in JavaScript:



function myFunction(one, two = 1) {
// code

}

myFunction("foo", "2");

myFunction("bar");


I tried this and it doesn't work. I don't know how to call this type of parameters, could somebody point me in the right direction?



Thanks.



Answer



function foo(a, b)
{
a = typeof a !== 'undefined' ? a : 42;
b = typeof b !== 'undefined' ? b : 'default_b';
//...
}


Possibly duplicate of Set a default parameter value for a JavaScript function



No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...