Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Convert NaN to a 0?

When a value is not entered in one of my forms, it creates a later result of NaN in the javascript. I just want to declare anytime NaN would show that it would instead show 0. I thought that by simply adding

<script>

a = a || 0

</script>

to the beginning of my scripts that it would be fine.

Is there anyway to say that NaN will just always equal 0, no matter what the situation?

like image 414
user2339898 Avatar asked May 02 '13 15:05

user2339898


1 Answers

NaN is a global object that you cannot override.

Reference: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/NaN

One alternative is to use isNaN then

a = isNaN(a) ? 0 : a;
like image 162
Terry Young Avatar answered Oct 17 '22 23:10

Terry Young