Here is an example where I use the || operator to assign a default value if the argument is undefined.
output = function (data) {
console.log("data.flag =", data.flag);
}
main = function (flag) {
output({
flag: flag || true;
});
}
main(true); // data.flag = true
main(false); // data.flag = true
However, the operation is ignored so always that the default value is assigned. How can I fix this? Here on JSFiddle - you have to open the console to see the output.
flag || truewill always be equal totrueifflagis boolean.