Logical operator || in javascript, 0 stands for Boolean false?
||
—expr1 || expr2
(Logical OR)Returns expr1 if it can be converted to true; otherwise, returns expr2. Thus, when used with Boolean values, || returns true if either operand is true; if both are false, returns false..
&&
—expr1 && expr2
(Logical AND)Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
The following values are equivalent to false in conditional statements:
- false
- null
- undefined
- The empty string
""
(\''
) - The number 0
- The number NaN
So...
var test = 0 || -1 ;
returns -1
.If it was var test = 0 || false || undefined || "" || 2 || -1
it would return 2
Logical operator on MDN
Use logical operator to assign a value or 0, otherwise assign alternative
Yes, ||
checks for the first operand to be falsy (false
when coerced to a boolean), and 0
is exactly that.
If you want to check for null
and undefined
only, you can use the nullish-coalescing operator:
const qux = foo ?? bar;
If you want to check for exactly undefined
, you should use the conditional operator as you did. Why does 0 && true return 0 in javascript instead of a boolean?
The documentation about the &&
operator says:
This is why is returns the first value:
expr1 && expr2
: Returnsexpr1
if it can be converted tofalse
; otherwise, returnsexpr2
.
0
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Description
You expected as a result false
(a boolean), however the boolean value of the resulting value is falsy too. This means that you can use it in a condition (if
) and have the expected behavior.
If you need to store it in a variable and prefer to have a boolean value, you can convert it. This can be done using a double negation: !!
!!(0 && true)
Or using Boolean
:
Boolean(0 && true)
Prevent value of 0 evaluating to false when using logical OR
You can do this with destructuring assignment:
let { Score = 'not set' } = Session;
If it's not set:const Session = { };let { Score = 'not set' } = Session;console.log( Score );
In JavaScript, why is 0 equal to false, but when tested by 'if' it is not false by itself?
The reason is because when you explicitly do "0" == false
, both sides are being converted to numbers, and then the comparison is performed.
When you do: if ("0") console.log("ha")
, the string value is being tested. Any non-empty string is true
, while an empty string is false
.
Equal (==)
If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
(From Comparison Operators in Mozilla Developer Network)
Why don't logical operators (&& and ||) always return a boolean result?
var _ = ((obj.fn && obj.fn() ) || obj._ || ( obj._ == {/* something */}))? true: false
will return boolean.UPDATE
Note that this is based on my testing. I am not to be fully relied upon.
It is an expression that does not assign true
or false
value. Rather it assigns the calculated value.
Let's have a look at this expression.
An example expression:
var a = 1 || 2;
// a = 1
// it's because a will take the value (which is not null) from left
var a = 0 || 2;
// so for this a=2; //its because the closest is 2 (which is not null)
var a = 0 || 2 || 1; //here also a = 2;
Your expression:var _ = (obj.fn && obj.fn() ) || obj._ || ( obj._ = {} );
// _ = closest of the expression which is not null
// in your case it must be (obj.fn && obj.fn())
// so you are gettig this
Another expression:var a = 1 && 2;
// a = 2
var a = 1 && 2 && 3;
// a = 3 //for && operator it will take the fartest value
// as long as every expression is true
var a = 0 && 2 && 3;
// a = 0
Another expression:var _ = obj && obj._;
// _ = obj._
Related Topics
Javascript: Dynamically Creating Variables for Loops
How to Detect When a Tab Is Focused or Not in Chrome with JavaScript
JavaScript Add Method to Object
Problems with Circular Dependency and Oop in Angularjs
Pass Variables to JavaScript in Expressjs
Get Visitors Language & Country Code with JavaScript (Client-Side)
Object Doesn't Support This Property or Method Rails Windows 64Bit
Start HTML5 Video at a Particular Position When Loading
Loading Local Files with JavaScript Without a Web Server
When Do I Need to Specify the JavaScript Protocol
How Do We Set Remote in Typeahead.Js
JavaScript Contenteditable - Set Cursor/Caret to Index
Can't Set Innerhtml on Tbody in Ie