JavaScript Alerts | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

2nd Jan 2022, 6:36 PM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
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.
3rd Jan 2022, 1:39 AM
Simba
Simba - avatar
+ 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.
3rd Jan 2022, 2:25 AM
Simba
Simba - avatar
+ 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.
3rd Jan 2022, 1:58 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar
+ 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
3rd Jan 2022, 2:34 AM
Tahiti🌍Castillo
Tahiti🌍Castillo - avatar