scala.js

Scala.js - Handling NaN


import scala.scalajs.js
import scala.scalajs.js.Date
import org.scalajs.dom.window.alert

val num: Double = new Date("a").getTime
alert((num + 1).toString)

This code reports NaN as the value of the alert. How do I know beforehand that num is a NaN in Scala.js code and not a proper Double?


Solution

  • Unless I misunderstood the question, you can test for NaN with the isNaN method:

    if (num.isNaN)
      println("it is NaN")