Can I use chained comparison operator syntax? [duplicate] - javascript

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) { }

Related

In JavaScript why 2 > 1 > 0 true while 8 > 1 > 1 false? [duplicate]

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.

Comparison operators in Javascript

Why when I run in Chrome console this snippets of code I get different results?
1 < 2 < 3
true
3 > 2 > 1
false
Javascript doesn't support expressions like 0 < x < 10. Because the < and > operators have equal precedence and are applied left to right, in effect what is being evaluated is (1 < 2) < 3.
Because the expression (1 < 2) evaluates to true, the second < operation is, in effect, evaluating whether true is less than or equal to 3.
1 < 2 < 3
(1 < 2) < 3
true < 3
true
Your second example can be understood by applying the same procedure:
3 > 2 > 1
(3 > 2) > 1
true > 1
false
As for why true < 3 evaluates to true and true > 1 evaluates to false, the answer is a bit more complex, and has to do with the loose typing system in Javascript. In short, when compared with a number, javascript's true value evaluates to 1, and its' false value evaluates to 0.
This chapter on the substitution method of procedure application is fairly helpful. https://mitpress.mit.edu/sicp/full-text/sicp/book/node10.html
Because programming is not math.
1 < 2 returns boolean true which is then implicitly converted to a number 1 which is indeed less that three. Same thing happens in the second example but 1 > 1 returns false.

How does the identity operator in JavaScript work?

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"

Why does 1==1==1 return true, "1"=="1"=="1" return true, and "a"=="a"=="a" return false? [duplicate]

This question already has answers here:
How does (A == B == C) comparison work in JavaScript?
(6 answers)
Closed 8 years ago.
function a() { return (1 == 1 == 1); }
function b() { return ("1" == "1" == "1"); }
function c() { return ("a" == "a" == "a"); }
I tested the above code in Chrome's console and for some reason, a() returns true, b() returns true, and c() returns false.
Why is this so?
Because you are comparing the (boolean) result of the first equality with the (non-boolean) third value.
In code, 1 == 1 == 1 is equivalent to (1 == 1) == 1 is equivalent to true == 1.
This means the three methods can be written more simply as:
function a() { return (true == 1); }
function b() { return (true == "1"); }
function c() { return (true == "a"); }
These comparisons work according to these rules (emphasis mine):
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 string operand is
converted to a number if possible. If both operands are objects, then
JavaScript compares internal references which are equal when operands
refer to the same object in memory.
So what happens in c is that "a" is converted to a number (giving NaN) and the result is strictly compared to true converted to a number (giving 1).
Since 1 === NaN is false, the third function returns false. It's very easy to see why the first two functions will return true.
Because 1 == true
But "a" != true
So basically what happens is that
1 == 1, "1" == "1" and "a" == "a" are all evaluated to be true and then compared to the next value.
The string "1" is converted to a number (1) prior to being compared to true and is thus also considered to be equal to true.
Now, the "WHY?!?!" question is explained by the fact that Javascript has its roots in the C-family of languages. In which any number, other than 0 is considered to be a valid true boolean. :-/
Because 1 and "1" are both converted to true, as numbers. This is not the case with "a". Therefore:
("1" == "1" == "1")
evaluates to
(("1" == "1") == "1")
which evaluates to
(true == "1")
Similarly,
("1" == 1 == "1")
is also true, or any combination thereof. In fact, any non-zero number when converted to a boolean is true.
Whereas, "a" does not evaluate to true.
It's because JavaScript is a weakly typed language. This means that it is not expressive enough to talk about types, and in fact implicitly coerces values to belong in types to which they have no semantic relation. So, (1 == 1) == 1 evaluates to true because (1 == 1) correctly evaluates to true, so that JavaScript evaluates (true) = 1. In particular, it is turning 1 to a boolean (or true to a number -- I forget which, but the result is effectively the same).
The point is that JavaScript is turning one type of value into another type of value behind your back.
Your question shows why this is a problem: ('a' == 'a') == 'a' is false, because ('a' == 'a') is true, and JavaScript ends up comparing (true) == 'a'. Since there is just no sensible way to turn a Boolean into a letter (or a letter into a boolean), that statement is false. Of course, that breaks referential transparency for (==).
It's true that (1 == 1) == 1. Then it will be true == 1, but not in a == a == a.
Boolean handled as bits, each bit stands for true or false ( 1 for true, 0 for false )
so that 1 stands for true, and 0 stand for false
and 1 == 1 == 1 will be like (1 == 1) == 1, true == 1, true
while 'a' == 'a' == 'a' will be ('a' == 'a') == 'a', true == 'a', false
BONUS: 0 == 1 == 0, 1 == 0 == 0 and 0 == 0 == 1 returns true

Should I use == or === In Javascript? [duplicate]

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.

Categories