function test(ab)
{
if (ab === undefined) alert('undefined')
else alert(ab);
}
test();
function myfunc(a, b)
{
// use this if you specifically want to know if b was passed
if (b === undefined) {
// b was not passed
}
// use this if you know that a truthy value comparison will be enough
if (b) {
// b was passed and has truthy value
} else {
// b was not passed or has falsy value
}
// use this to set b to a default value (using truthy comparison)
b = b || "default value";
}
function myfunc(a)
{
var b;
// use this to determine whether b was passed or not
if (arguments.length == 1) {
// b was not passed
} else {
b = arguments[1]; // take second argument
}
}
b === void 0;
typeof b === 'undefined'; // also works for undeclared variables
function multiply(a, b = 1) {
return a * b;
}
multiply(5, 2)
multiply(5)
--------------------------------------------------
function findMax() {
var i;
alert(arguments[0][1]);
/* var max = -Infinity;
for(i = 0; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i];
}
}
return max;
*/
}
document.getElementById("demo").innerHTML = findMax([4,7]);