Comparison numbers in Javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 2

Comparison numbers in Javascript Why is the number 30. greater than the number 30?

3rd Apr 2020, 7:42 PM
Ignat - avatar
1 Answer
+ 2
Because you are comparing string, not numbers, and they are compared based on their char code value (so "5" > "51" but 5 < 51). You need to cast your inputs to number, either explicitly or implicitly, with the use of parseFloat (or parseInt: explicit) or an implicit cast: var x = "42"; console.log(typeof x); // string x = +x; console.log(typeof x); // number There are few differences between implicit and explicit methods, but in major cases, you could use any of them ;)
3rd Apr 2020, 8:07 PM
visph - avatar