What Does The Or Operator Do In This Bit Of Javascript?
Solution 1:
If type
is a "falsey" value, then the value of callback
will be assigned to the type
variable, otherwise type
will be assigned.
The "falsey" values are:
false
null
undefined
0
""
(empty string)NaN
So basically it says "replace type
with callback
if type
is any one of the falsey values".
Consider this:
vartype = undefined;
type = type || "default value";
The type
variable will ultimately get "default value"
assigned.
If it was like this:
vartype = "some value";
type = type || "default value";
Then it would keep its "some value"
.
Solution 2:
It sets the variable "type" to either its current value, or the value of "callback" if the current value is not "truthy". If "type" is undefined
, or null
, or 0, or the empty string, or boolean false
, then it'll be set to the value of "callback".
edit oops or NaN
Solution 3:
So I see multiple variables can be 'chained' together and the first "non-falsey" value is assigned.
varval, a, b, c;
a = undefined;
b = null;
c = "C";
val = a || b || c;
alert(val);
That's pretty damn handy.
Post a Comment for "What Does The Or Operator Do In This Bit Of Javascript?"