Search code examples
javascriptfloating-pointnanmilliseconds

NaN JavaScript Result


I am getting a NaN result. Clearly something is not right. But I cannot figure is out what is wrong in the calculations. Heres the code:

var totalTime = video.duration;
var milliToHours = function (value) {
  return value / 60 / 60;
}
milliToHours (totalTime);
// howerver it returns NaN in console

Solution

  • video.duration must be undefined at function execution. Add a console.log(value); statement to your function.

    var totalTime;
    var milliToHours = function(value) {
      console.log(value);
      return value / 60 / 60;
    }
    console.log(milliToHours(totalTime)); // undefined => NaN
    totalTime = null;
    console.log(milliToHours(totalTime)); // null => 0
    totalTime = 39856.6757;
    console.log(milliToHours(totalTime)); // 39856.6757 => 11.071298805555555