If you are simply verifying the likelihood of b
, you can do this:
var a = b || 40;
... which is shorter and (perhaps) more obvious. In JavaScript ||
is a short circuit operator. He returns the left side if it is true, otherwise it returns the right side. (i.e., it does not return a boolean if the input is not boolean).
If you want to determine if b
really is, then you are better off:
var a = (typeof b !== "undefined") ? b : 40;
source share