admin管理员组

文章数量:1406063

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?

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?

Share Improve this question asked May 2, 2013 at 15:06 user2339898user2339898 915 bronze badges 4
  • isNaN() exists so you don't have to convert it to 0. – Jordan Commented May 2, 2013 at 15:11
  • @Jordan You missed the point of the question. The OP wants NaN to always equal 0. – Danny Beckett Commented May 2, 2013 at 15:13
  • Right.. Why? Just use isNaN(). – Jordan Commented May 2, 2013 at 15:14
  • @Jordan I suggest you re-read the question thoroughly. – Danny Beckett Commented May 2, 2013 at 15:15
Add a ment  | 

1 Answer 1

Reset to default 8

NaN is a global object that you cannot override.

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

One alternative is to use isNaN then

a = isNaN(a) ? 0 : a;

本文标签: javascriptHow do I Convert NaN to a 0Stack Overflow