0
JavaScript Alerts
Iām almost halfway done with the JavaScript course, but Sololearn challenged me and I got this question wrong: alert(āNā>āBā||ā1ā===1||āEā<āZā); True or false? Please explain your answer. Thank you.
4 Answers
+ 6
True.
Rational operators(<,>,===,==) have higher operator precedence than logical or operator.
By comparing ascii values "N" > "B" and "E" < "Z" are true statements while "1" === 1 statement is false.
So, alert(true || false || true) also returns true.
+ 4
1. To know the result of operation with strings(or characters), you need to see the ASCII Table (The ASCII Table, is to make the computer understanding what you are writing, because it can only understand numbers, so the table give to each symbol a number)
If you see the ASCII Table, the number for "B" is 66 and for "N" is 78. So the computer will compare these two values, 66 and 78.
So, if you put for example: alert("N" > "B")
Actually, the computer will understand as: alert(78> 66)
Which give the result of: true
2. Logical or operator returns the first Truthy value if there are any, else return the last value in the expression and it's executed left to right.
So, (true || false) evaluates true and the same goes to the rest.
+ 1
Simba , thanks for helping me with this. I think I understand most of it. I have two questions.
1. The reason that N>B and E<Z, is it because of the order of letters in the alphabet? Latter letters are greater? Like A is least and Z is greatest?
2. If the alert is True||False||True, how does it result in true when we have one false? I donāt remember this from the Boolean lesson.
Thanks again.
+ 1
Simba I did not know this at all. Iāve located an ASCII table and will share it here, in case it may help someone else.
Thank you!
https://www.rapidtables.com/code/text/ascii-table.html