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
  • 609
  • 7
  • 21
Govind Samrow
  • 9,981
  • 13
  • 53
  • 90

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
  • 22,145
  • 4
  • 29
  • 53