LoginSignup
3
2

More than 5 years have passed since last update.

JavaScriptで0÷0はNaNになるよ!

Last updated at Posted at 2016-06-07

ある日、現場のソースを見てみたらこんなのを見つけた。

※簡略化してます

sample
function rate (a, b) {
  var c = a / b;
  return c;
}

これ、aとb共に0ならどうなるの?ってことで試してみました。

sample1
console.log(rate(0, 0));
// => NaN

NaN(Not A Number)になりました。。。
ちゃんと値チェックをいれないとダメですね。

おまけ

sample2
console.log(rate(10, 0));

aが0以外かつbが0だと、Infinityになりました。
・a が正の数: Infinity
・a が負の数: -Infinity

3
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2