Given this snippet of JavaScript...
var a;
var b = null;
var c = undefined;
var d = 4;
var e = 'five';
var f = a || b || c || d || e;
alert(f); // 4
Can someone please explain to me what this technique is called (my best guess is in the title of this question!)? And how/why it works exactly?
My understanding is that variable f
will be assigned the nearest value (from left to right) of the first variable that has a value that isn't either null or undefined, but I've not managed to find much reference material about this technique and have seen it used a lot.
Also, is this technique specific to JavaScript? I know doing something similar in PHP would result in f
having a true boolean value, rather than the value of d
itself.
This question has already received several good answers.
In summary, this technique is taking advantage of a feature of how the language is compiled. That is, JavaScript "short-circuits" the evaluation of Boolean operators and will return the value associated with either the first non-false variable value or whatever the last variable contains. See Anurag's explanation of those values that will evaluate to false.
Using this technique is not good practice for several reasons; however.
Documented Features: There is an existing alternative that meets this need and is consistent across more languages. This would be the ternary operator:
() ? value 1: Value 2.
Using the ternary operator does require a little more typing, but it clearly distinguishes between the Boolean expression being evaluated and the value being assigned. In addition it can be chained, so the types of default assignments being performed above could be recreated.
It means that if
x
is set, the value forz
will bex
, otherwise ify
is set then its value will be set as thez
's value.it's the same as
It's possible because logical operators in JavaScript doesn't return boolean values but the value of the last element needed to complete the operation (in an OR sentence it would be the first non-false value, in an AND sentence it would be the last one). If the operation fails, then
false
is returned.It's setting the new variable (
z
) to either the value ofx
if it's "truthy" (non-zero, a valid object/array/function/whatever it is) ory
otherwise. It's a relatively common way of providing a default value in casex
doesn't exist.For example, if you have a function that takes an optional callback parameter, you could provide a default callback that doesn't do anything:
This is made to assign a default value, in this case the value of
y
, if thex
variable is falsy.The boolean operators in JavaScript can return an operand, and not always a boolean result as in other languages.
The Logical OR operator (
||
) returns the value of its second operand, if the first one is falsy, otherwise the value of the first operand is returned.For example:
Falsy values are those who coerce to
false
when used in boolean context, and they are0
,null
,undefined
, an empty string,NaN
and of coursefalse
.Its called Short circuit operator.
Short-circuit evaluation says, the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression. when the first argument of the OR (||) function evaluates to true, the overall value must be true.
It could also be used to set a default value for function argument.`
According to the Bill Higgins' Blog post; the Javascript logical OR assignment idiom (Feb. 2007), this behavior is true as of v1.2 (at least)
He also suggests another use for it (quoted): "lightweight normalization of cross-browser differences"