Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1
This behaviour is called Coercion. If you compare a string to a number the, string will be converted to a boolean (true if the string is not empty, false otherwise) and that boolean again is converted to a number (1 if the boolean is true, 0 otherwise). In the case of your code the string "ghh" is converted to "true" and inturn converted to "1" which becames: if (8 > 1) { // Will execute because the condition is true. } else { // Will not execute because the condition is not false. }
3rd Nov 2019, 1:06 AM
Ryan
Ryan - avatar