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.
See short-circuit evaluation for the explanation. It's a common way of implementing these operators; it is not unique to JavaScript.
Return output first true value.
If all are false return last false value.
Example:-
Javascript variables are not typed, so f can be assigned an integer value even though it's been assigned through boolean operators.
f is assigned the nearest value that is not equivalent to false. So 0, false, null, undefined, are all passed over:
It will evaluate X and, if X is not null, the empty string, or 0 (logical false), then it will assign it to z. If X is null, the empty string, or 0 (logical false), then it will assign y to z.
Will output 'bob';
There isn't any magic to it. Boolean expressions like
a || b || c || d
are lazily evaluated. Interpeter looks for the value ofa
, it's undefined so it's false so it moves on, then it seesb
which is null, which still gives false result so it moves on, then it seesc
- same story. Finally it seesd
and says 'huh, it's not null, so I have my result' and it assigns it to the final variable.This trick will work in all dynamic languages that do lazy short-circuit evaluation of boolean expressions. In static languages it won't compile (type error). In languages that are eager in evaluating boolean expressions, it'll return logical value (i.e. true in this case).
Javacript uses short-circuit evaluation for logical operators
||
and&&
. However, it's different to other languages in that it returns the result of the last value that halted the execution, instead of atrue
, orfalse
value.The following values are considered falsy in JavaScript.
""
(empty string)Ignoring the operator precedence rules, and keeping things simple, the following examples show which value halted the evaluation, and gets returned as a result.
The first 5 values upto
NaN
are falsy so they are all evaluated from left to right, until it meets the first truthy value -"Hello"
which makes the entire expression true, so anything further up will not be evaluated, and"Hello"
gets returned as a result of the expression. Similarly, in this case:The first 5 values are all truthy and get evaluated until it meets the first falsy value (
null
) which makes the expression false, so2010
isn't evaluated anymore, andnull
gets returned as a result of the expression.The example you've given is making use of this property of JavaScript to perform an assignment. It can be used anywhere where you need to get the first truthy or falsy value among a set of values. This code below will assign the value
"Hello"
tob
as it makes it easier to assign a default value, instead of doing if-else checks.You could call the below example an exploitation of this feature, and I believe it makes code harder to read.
Inside the alert, we check if
messages
is falsy, and if yes, then evaluate and returnnoNewMessagesText
, otherwise evaluate and returnnewMessagesText
. Since it's falsy in this example, we halt at noNewMessagesText and alert"Sorry, you have no new messages."
.