- 2

Comparison numbers in Javascript

https://code.sololearn.com/W7kOWHT32RWq/?ref=app Why is the number 30. greater than the number 30?

3rd Apr 2020, 7:42 PM
Ignat
Ignat - avatar
1 Odpowiedź
+ 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
visph - avatar