0

Checking if value of element is greater then or equal to Zero(0) but in case of empty string its result true

    var amount = $(this).text();
    if (amount >= 0) {
        $(this).text(parseInt(amount).toLocaleString());
    }

if amount = "" then result is NaN why?

J Shubham
  • 591
  • 6
  • 21
Govind Samrow
  • 9,455
  • 13
  • 50
  • 85

1 Answers1

2

Yes .because amount is a string on the time of validation .Empty space also have length

"" == 0

Error

var amount=""
console.log(amount >=0)

FIX

Try to validate simple if(var) .Use trim() remove unwanted empty spaces

var amount = $(this).text().trim();
    if (amount) {
        $(this).text(parseInt(amount).toLocaleString());
    }
prasanth
  • 21,342
  • 4
  • 27
  • 50