Reputation: 2394
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?
Upvotes: 6
Views: 3490
Reputation: 89
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
Upvotes: 0
Reputation: 647
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:
Short-circuit evaluation
As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:
Upvotes: 5
Reputation: 1274
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.
Upvotes: 0
Reputation: 3424
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
Upvotes: 1
Reputation: 1833
Because operator &&
return first falsey element otherwise they return last element
1 && 0 && false // 0
1 && 2 && 3 // 3
Upvotes: 9
Reputation: 74
(something falsy) && (anything) will always return false.
Also 0 is falsy and '0' (a non empty string) is truthy
Upvotes: 0
Reputation: 72186
The JavaScript documentation of logical operators explains:
Logical operators are typically used with
Boolean
(logical) values. When they are, they return aBoolean
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.
Upvotes: 1