How do comparison operator in JavaScript work? Comparing string to string, string to number

The problem was that I didn’t understand logic of operation (a + b < 4). I thought that since there is operator “<” all the operands will be transformed into numbers, but actually a + b summarizes strings and only after that it does the transformation. I know it’s lame but this is just 4th day of learning JS. Now I get the importance of prioritization and data types.

This is working correct now. I’ll be happy if somebody suggests a better (shorter) solution:

let a = prompt("Number for a", "1");
let b = prompt("Number for b", "2");
a = Number(a);
b = Number(b);

result = (a + b > 4) ? 'A lot' : 'Not enough';
alert(result);

Leave a Comment