Default value for a boolean parameter in JavaScript function

See following example;

You could skip the ternary, and evaluate the “not not x”, e.g. !!x.

If x is undefine, !x is true, so !!x becomes false again. If x is true !x is false so !!x is true.

function logBool(x) {
    x = !!x;
    console.log(x);
}

var a, b = false, c = true;
logBool(a); // false
logBool(b); // false
logBool(c); // true

https://stackoverflow.com/questions/24332839/set-a-default-value-for-a-boolean-parameter-in-a-javascript-function

FavoriteLoadingAdd to favorites
Spread the love

Author: Shahzad Khan

Software developer / Architect