Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen and why number evaluates to NaN, after multiplying, in Javascript?
    primarykey
    data
    text
    <p>From my experience I know, that I can get <code>NaN</code> when doing some division or when trying to make a number out of string, when it actually does not contain a number. Are there any other situations, when I can get a <code>NaN</code>. In particular -- is it possible to get it from multiplication?</p> <p>This is a piece of code, that I use in my PhoneGap application:</p> <pre><code>var g = 9.80665; acceleration.x = (acceleration.x * g).toFixed(2); acceleration.y = (acceleration.y * g).toFixed(2); acceleration.z = ((acceleration.z + 1) * g).toFixed(2); </code></pre> <p>An <code>acceleration</code> is a <a href="http://docs.phonegap.com/en/2.9.0/cordova_accelerometer_accelerometer.md.html#Acceleration" rel="nofollow noreferrer">base PhoneGap object</a> and I can hardly believe, that it contains a string or any other value, except float, that could result in a <code>NaN</code>.</p> <p>However, in some certain situations (on some specific devices) I'm getting a <code>NaN</code> out of above.</p> <p>It isn't a problem for me to "secure" above values with a code like this:</p> <pre><code>acceleration.x = (parseFloat(acceleration.x) || 0) + ' m/s\u00b2'; acceleration.y = (parseFloat(acceleration.y) || 0) + ' m/s\u00b2'; acceleration.z = (parseFloat(acceleration.z) || 0) + ' m/s\u00b2'; </code></pre> <p>But, I'm really curious, when and why I can get a <code>NaN</code> after doing just a multiply of some values?</p> <p>BTW: I've read <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN#Description" rel="nofollow noreferrer">this</a> at MDN, as good as <a href="https://stackoverflow.com/questions/12227594/which-is-better-numberx-or-parsefloatx">many</a> <a href="https://stackoverflow.com/questions/3215120/why-javascript-says-that-a-number-is-not-a-number">related</a> <a href="https://stackoverflow.com/questions/559792/what-is-the-reserved-keyword-for-nan-in-javascript">answers</a> <a href="https://stackoverflow.com/questions/6678596/nan-is-a-number">here</a> at Stack Overflow, but to brought me no help or answer to my question.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload