This question already has answers here:
JavaScript OR (||) variable assignment explanation
(12 answers)
Closed 6 years ago.
Why the console shows "2" and "false" in this expressions?
var a = '' || 0 || 2 || true || false;
var b = 3 && true && false && null;
console.log (a,b);
You got those results because it's logical comparison here, using logical operators && and ||, if you take a look at MDN Specification of Logical Operators, you will see that:
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 can be converted to true; if both can be converted to false, returns false.
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 can be converted to true; otherwise, returns false.
So in your case:
For the first expression:
var a = '' || 0 || 2 || true || false;
It will return 2 because it's the first oprand that's evaluated to true.
And for the second one:
var b = 3 && true && false && null;
It will return false as one of its operand is false.
Related
I happened to know the following code
Here is the code, and very simple:
var test = 0 || -1 ;
console.log(test);
then the output in the console is -1
and somehow i am really new into the javascript,
all i think of is that the 0 stands for Boolean False in JS ,and so || operator seems to ignore the 0 and assign the value -1 to the variable
so am i right ?
i just want a confirm
|| — 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.
All values in Javascript are either "truthy" or "falsy".
The following values are equivalent to false in conditional statements:
false
null
undefined
The empty string "" (\ '')
The number 0
The number NaN
All other values are equivalent to true.
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
You can use Nullish coalescing operator (??)
The nullish coalescing operator (??) is a logical operator that
returns its right-hand side operand when its left-hand side operand is
null or undefined, and otherwise returns its left-hand side operand.
Only null and undefined will be falsey. 0 will be considered true. But take care: an empty string will be considered true too!
console.log('0 ?? 1 ->', 0 ?? 1) // expected output: 0
console.log('null ?? 1 -> ', null ?? 1) // expected output: 1
console.log('undefined ?? 1 ->', undefined ?? 1) // expected output: 1
console.log('"" ?? 1 ->', "" ?? 1) // expected output: ""
This question already has answers here:
What does "!" operator mean in javascript when it is used with a non-boolean variable?
(5 answers)
Closed 2 years ago.
I looked a lot, but I couldn't find an answer for this especific case.
Why does this expression return true?
let variable = 0
!variable // true
I understand that the ! mark checks if a value is null or undefined, but in this case variable is defined. This is tricking me.
Isn't 0 really considered a valid value?
! is known as the logical NOT operator. It reverses the boolean result of the operand (or condition)
0 is also considered as the boolean false, so when you use !variable you are using the logical operator and saying it to change the value of the variable to its opposite, that in boolean is true
0 == false == !1 == !true
1 == true == !0 == !false
in Javascript are considered false:
false, null, undefined, "", 0, NaN
are considered true:
true, 1, -0, "false". <- the last one is a not empty string, so its true
if( false || null || undefined || "" || 0 || NaN) //never enter
if( true && 1 && -1 && "false") //enter
https://developer.mozilla.org/en-US/docs/Glossary/Falsy
To quote MDN Web docs, the Logical NOT !:
Returns false if its single operand can be converted to true; otherwise, returns true
So in your case it returns true because 0 can be converted to false
You can check out this link:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
As title
As I know
(if this part is true) && (this part will execute)
if(condition){
(this part will execute)
}
0 is false, so why not echo false but 0?
Because operator && return first falsey element otherwise they return last element
1 && 0 && false // 0
1 && 2 && 3 // 3
From MDN:
expr1 && expr2 -- 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.
expr1 || expr2 -- 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.
!expr -- Returns false if its single operand can be converted to true; otherwise, returns true.
Some expressions that can be converted to false are:
null
NaN
0
empty string("" or '' or ``)
undefined
Short-circuit evaluation
As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:
false && (anything) is short-circuit evaluated to false.
true || (anything) is short-circuit evaluated to true.
The JavaScript documentation of logical operators explains:
Logical operators are typically used with Boolean (logical) values. When they are, they return a Boolean value. However, the && and || operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.
In javascript all except for null, undefined, false, 0, and NaN are Truthy.
In your case, why not echo false but 0?.
Javascript's ToBoolean function evaluates it to the first falsey value. i.e,
0 && true
=> 0
true && undefined
=> undefined
null && undefined
=> null
And if you need either strictly true or false, then go for not-not i.e, !!.
!!0 && true
=> false
!!true && undefined
=> false
!!null && undefined
=> false
(something falsy) && (anything) will always return false.
Also 0 is falsy and '0' (a non empty string) is truthy
a && b is equivalent to
if (a) {
return b;
} else {
return a;
}
So in your case a equals to 0, which is falsy, therefore you get a.
Unlike other languages, JavaScript does not return true or false on && and ||, it returns the first truthy operand for a || operator, or the last one, and the first falsy operand for the && operator, or the last one.
You can find more info here.
For && operator comparing with false is always FALSE.
0 && false => 0
false && 0 => false
for better understanding :-
In case of && operator (always start from left-right), when you get the value 0 (false) it will print 0(false); if start with false it will directly print false. it won't check the second operand when get false.
but in case
true && 1 => 1
1 && true => true
as it has to check till end and ultimately give the end operand as result if won't get false.
For || operator comparing with true is always TRUE.
1 || true => 1
true || 1 => true
for better understanding :-
In case of || operator (always start from left-right), when you get the value 1 (true) it will print 1(true).
Starting with true it will directly print true. it won't check the second operand when get true.
but in case
false || 1 => 1
0 || true => true
This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
Closed 4 years ago.
I had checked for many test cases. It's looking if the first value is true and it's returning the second value.
Example:
2 && 5 // --> 5
5 && 10 // --> 10
0 && 2 // --> 0
Why doesn't it return either True or 1?
Per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Description,
Operator: Logical AND (&&)
Usage: expr1 && expr2
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 && operator will return the last value if all other values are truthy, otherwise it will return the first non truthy value. So, as in 0 && 2, 0 is non-truthy, that gets returned.
Having the following test case: (find fiddle here)
var a = new Date();
var b = null;
var c = {test: "test"};
if(a)
console.log(a); //--- prints the current date
if(b)
console.log('null'); //--- never reached
if(c)
console.log('test'); //--- prints 'test'
console.log(a && b); //--- prints null
Knowing that
console.log(typeof null); //--- prints "object"
console.log(typeof c); //--- prints "object"
I expect the result of
console.log(a && b);
to be false and not null as it shown in the example.
Any hint?
From the MDN:
expr1 && expr2 : Returns expr1 if it can be converted to false; otherwise, returns expr2
new Date can't be converted to false (it's not falsy), so b is returned.
I expect the result of
console.log(a && b);
to be false and not null as it shown in the example.
In many languages with && and || operators (or AND and OR), the result is always a boolean, yes.* But JavaScript's && and || are more useful than that: Their result is their left-hand operand's value or their right-hand operand's value, not coerced to boolean.
Here's how && works:
Evaluate the left-hand operand.
If the value is falsey (coerces to false when we make it a boolean), return the value (not the coerced value)
If the value from #2 is truthy, evaluate and return the right-hand value (uncoerced)
The falsey values are null, undefined, 0, "", NaN, and of course, false. Everything else is truthy.
So for instance
console.log(0 && 'hi'); // 0
...shows 0 because 0 is falsey. Note it resulted in 0, not false. Similarly, the result here:
console.log(1 && 'hello'); // hello
...is 'hello' because the right-hand side isn't coerced at all by &&.
|| works similarly (I call it the curiously-powerful OR operator): It evaluates the left-hand side and, if that's truthy, takes that as its result; otherwise it evaluates the right-hand side and takes that as its result.
The only time you get true or false from && or || is if the selected operand is already a boolean.
* Of course, many (but not all) of those languages (Java, C#) also require that the operands to && and || already be booleans. C's an exception, it does some coercion, but still has the result of the operands as a boolean (or if you go back far enough, an int that will be 1 or 0).
From MDN:
Logical AND (&&) expr1 && expr2 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.
MDN Documentation