This code doesn’t work:
var number = $(this).find('.number').text();
var current = 600;
if (current > number){
// do something
}
HTML:
<div class="number">400</div>
Seems there is some problem with converting text() from text-like value to number.
What is the solution?
Always use
parseIntwith a radix (base) as the second parameter, or you will get unexpected results:A popular variation however is to use
+as a unitary operator. This will always convert with base 10 and never throw an error, just returnzeroNaNwhich can be tested with the functionisNaN()if it’s an invalid number: