Why does (9 > 8) === true result in "true" but (10>9>8) === true results in "false"?
If we see both, the JavaScript executes from left to right. As we all know:
(9 > 8) === true
The above statement is indeed true. But what we need to know is, how JavaScript executes this. If we see how it gets executed, the execution steps are as below:
(10 > 9 > 8) === true
((10 > 9) > 8) === true
(true > 8) === true
(1 > 8) === true
false === true
false
Because:
10 > 9 > 8 === (true) > 8
Which is false. In truthy values, a true gets expressed as "1". Since true > 0 and false < 1.
You should only have one comparative operator without using the AND(&&) or the OR(||) operators.
If you want (10>9>8) to resolve to true, you would format it like this:
(10 > 9 && 9 > 8)
this says "check 10 > 9, if that's true, check 9 > 8. If they're both true, return true"
Related
This question already has answers here:
Why does (0 < 5 < 3) return true?
(14 answers)
Closed 1 year ago.
As we know about the greater than and less than operator that how they work.
console.log(2 < 12) // true
console.log(2 < "12") // true
console.log("2" > "12") // true
But when we compare more than 2 values with greater than and less than operator. Then I can't understand the logic behind the scene.
1. console.log(1 > 2 < 3) // true and console.log(1 > 2 > 3) // false
2. console.log(9 > 2 > 9 < 8 < 9) // true
3. console.log(9 > 8 > 7 > 9 > 5) // false
Can somebody please elaborate and explain behind the reason for 1 , 2 and 3 ?
I'd like to clear this problem using following example.
1 < 2 < 3 // evaluates to true
But
3 > 2 > 1 // evaluates to false
As we know js execute from left to right. Therefor, in first example when statement
1 < 2
is executed it evalutes to true. In programming true refers to 1 and false refers to zero. Then after executing the above statement we have true means 1. Now when this result is combined with next statement
true < 3
Then in short this statement means
1 < 3
because true refers to 1 in programming. Since 1 < 3 is true that's why we have the final result as true.
Coming to the next example 3 > 2 > 1 which evaluates to false because
3 > 2 // evaluates to true
Since above statement is true then in programming sense we got 1(true) and then by combining this result with next statement
true > 1 // evaluates to false
The above statement is in short
1 > 1
Since 1 is not greater than 1 therefor it returns the final result to false.
3 > 2 = true
true > 1 = false
So finally we conclude that by programming sense both statement evaluates to different results but in sense of mathematics both are same. Its a interview question. Hopefully you understand this.
Please let me know if needs any other information. In other case, It might be accepted answer.
This question already has answers here:
Why does "true" == true show false in JavaScript?
(5 answers)
Closed 2 years ago.
I'm trying to understand the following case:
> "1" === 1
false
> "1" == 1
true
> true === 1
false
> true == 1
true
> "true" === true
false
> "true" == true
false
Why javascript comparison ("true" == true) is false?
Also, what is the proper way to compare this one?
Question has been asked before here.
In essence, "true" is converted to NaN, while true is converted to 1 (which is a boolean. Hence, they differ.
The Boolean in the comparison is going to have an implicit conversion to Number (0/1) so 1=true gets true, but "true" == true (1), so the string can't be equal to Number, it gets false
This question already has answers here:
Javascript AND operator within assignment
(7 answers)
Closed 7 years ago.
I am checking for the values of AND operator in javascript, the below code for some reason returning 0. Can someone explain actual behavior of AND operator here?
var result = 88 && 6 && 0 && null && 9;
alert(result);
&& evaluates as the left hand side if the left hand side is false, otherwise it evaluates as the right hand side.
(88) && (6 && 0 && null && 9)
88 is true, so the right hand side is evaluated.
(6) && (0 && null && 9)
6 is true, so the right hand side is evaluated.
(0) && (null && 9)
0 is false, so it is the result.
This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Difference between == and === in JavaScript [duplicate]
(2 answers)
Closed 8 years ago.
I am learning Javascript with codecademy, and I was doing some comparisons, and for my code I did:
`console.log(1 == 2)`
and it returned False.
I also did:
`console.log(2*2 === 3)`
and that also returned False.
To check that I have not made a mistake, I did:
`console.log(1 == 1)`
and that returned True
The instructions tell me that === means equal to.
Are there any problems with using == instead of ===? And, which is better to use and why?
Thanks for any help you can give me!
Using == compares only the values, === compares the type of the variable also.
1 == 1 -> true
1 == "1" -> true
1 === 1 -> true
1 === "1" -> false, because 1 is an integer and "1" is a string.
You need === if you have to determine if a function returns 0 or false, as 0 == false is true but 0 === false is false.
It really depends on the situation. It's usually recommended to use === because in most cases that's the right choice.
== means Similar while
=== means Equal. Meaning it takes object type in consideration.
Example
'1' == 1 is true
1 == 1 is true
'1' === 1 is false
1 === 1 is true
When using == it doesn't matter if 1 is a Number or a String.
http://www.w3schools.com/js/js_comparisons.asp
== is equal to || x==8 equals false
=== is exactly equal to (value and type) || x==="5" false
meaning that 5==="5" false; and 5===5 true
After all, it depends on which type of comparison you want.
This question already has answers here:
Why does (0 < 5 < 3) return true?
(14 answers)
Closed 1 year ago.
In one JS library I saw such syntax:
if (val > 5 == t) { ... }
I tested this in console:
1 == 1 == 2 // false
2 > 1 == 1 // true
1 == 2 == 1 // false
1 == 1 == 1 // true
1 < 2 < 3 // true
1 > 2 > 3 // false
At first glance all correct. Can this be used?
1 == 1 == 2 // this
true == 2 // becomes this
1 == 2 // which becomes this, and is false
2 > 1 == 1 // this
true == 1 // becomes this
1 == 1 // which becomes this, and is true
...and so on.
If you're wondering about the conversion, you should do a search on the == operator, which uses the Abstract Equality Comparison Algorithm.
You not really comparing what you think you are comparing:
(1 == 1) == 2 // actually true == 2 which is false
(1 == 2) == 1 // actually false == 1 which is false
Which is why strict equality === will fail in all cases
Javascript does not support the chained comparison syntax used in mathematics:
1 < 2 < 3 // 1 is less than 2 which is less than 3.
Instead, it evaluates each comparison left to right, which can sometimes yield the same results as mathematical chained comparison, as do all your examples, but the process is different:
1 < 2 < 3 // "1 is less than 2" is true, true is 1, "1 is less than 3" is true.
// Javascript returns true.
3 < 2 < 1 // "3 is less than 2" is false, false is 0, "0 is less than 1" is true.
// Javascript returns true.
For this reason, it should be discouraged.
To answer your question, however, yes it can be used.
It's a correct syntax but not one I would recommend. What's happening, is probably:
if ((val > 5) == t) { ... }
I tested this in console:
(1 == 1) == 2 // false
(2 > 1) == 1 // true
(1 == 2) == 1 // false
(1 == 1) == 1 // true
(1 < 2) < 3 // true
(1 > 2) > 3 // false
With the boolean on the left implicitly converted to an int.
There is nothing to stop you from using such an adventurous syntax however do keep in mind that one of the most frequent reasons for having bugs inside JavaScript code is messing up operator precedence.
Therefore its strongly advised to explicitly define precedence by adding brackets to precedence groups in case they consist of more than simple mathematical expressions for which the precedence is clearly determinable.
Another concern is type coercion.
jslint output:
Error:
Problem at line 2 character 13: Expected '===' and instead saw
'=='.
if (val > 5 == t) { }