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
1 Answer
Reset to default 8NaN
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
版权声明:本文标题:javascript - How do I Convert NaN to a 0? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744957748a2634459.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论