What Is The Purpose Of "x = X || 0"?
Solution 1:
||
in a variable assignment is a common way to specify a default value. This is because of JavaScript's falsy values. In JavaScript, undefined
, null
, empty string and 0
all evaluate to false
in a boolean context.
For example:
var blah = undefined;
if (blah) {
console.log('got it!');
}
else {
console.log('not true!'); // this one outputs
}
Using ||
in an assignment is a way of saying "if defined, otherwise use this".
For this code,
functionvalues(b) {
this.b = b || 0;
}
we can use a truth table:
b this.b
------------------
55101000undefined0null0''0
The values really of interest are undefined
and null
. So what we really want is:
if (b !== undefined && b !== null) {
this.b = b;
}
else {
this.b = 0;
}
But this.b = b || 0
is much shorter to write.
Solution 2:
This is a pretty classic question that gets tossed around relatively frequently.
The main purpose of utilizing something like x = x || 0
is to ensure that x
will have a false value Boolean, as you've described, but rather than having null
or false
, you are returning 0
.
The reasoning behind using the above rather than say, x = x || false
is that you will end up returning 0 (much easier to manipulate integer or float based data) rather than the other values.
Everything you've suggested is relatively true. For example:
var x = x || false; // Falsevar y = x || 0; // 0var z = x || null; // Null
alert(x);
alert(y);
alert(z);
If you had a purpose to use a true
or false
value, your suggestion would be perfectly adequate.
Solution 3:
||
returns a boolean only when both of its operands are booleans. Otherwise, it returns (from left to right) the first operand that isn't "falsy" or the last one if it can't find any.
In that function, this.b
is meant to be a number but the b
argument to the function is optional (so it could be undefined
). To make this.b
always a number, they used that. So if you didn't pass b
, it'd look like:
this.b = undefined || 0
And this.b
will be set to zero because undefined
is always "falsy".
A resource on truthy vs falsy: http://www.sitepoint.com/javascript-truthy-falsy/
The logic behind logical operators: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
Solution 4:
These are all equivalent:
this.b = b || 0;
if (b) { this.b=b } else { this.b=0 }
this.b = b ? b : 0;
this.b = 0; if (b) this.b=b;
Obviously the first one is less typing.
The reason the first one works is described fairly well on the MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
In short:
The &&
and ||
operators have a special feature whereby the values are evaluated for truthyness but the value is returned rather than just true
or false
.
In your case, expr1 || expr2
, will return expr1
if it evaluates to true, otherwise expr2
will be returned.
Solution 5:
It is a way to define default value for a parameter.
If b
is undefined
then this.b
becomes 0
.
Post a Comment for "What Is The Purpose Of "x = X || 0"?"