LoginSignup
33
32

More than 5 years have passed since last update.

JavaScriptでfalseになるもの

Last updated at Posted at 2014-08-31

JavaScriptでfalseになるものを整理してみました。

・false
・空文字
・NaN
・数値の0
・undefined
・null

(注意)
Infinityは、無限大を表す数値なためfalsyではありません。
私は、間違えてInfinityはfalsyだと思っていました。
でも、Infinityは、無限大を表すんだから普通に考えるとtruthyですよね。
ちなみに、Infinityで割ったものは全て0です。

falsy.js
if ( !false ) {
    console.log('falseは、falsyです');
}

if ( !null ) {
    console.log('nullは、falsyです');
}

if ( !undefined ) {
    console.log('undefinedは、falsyです');
}

if ( !'' ) {
    console.log('空文字は、falsyです');
}

if ( !0 ) {
    console.log('0は、falsyです');
}

if ( !NaN ) {
    console.log('NaNは、falsyです');
}

//注意
if ( Infinity ) {
    console.log('Infinityは、truthyです');
}

以上、お役に立てれば幸いです。

33
32
6

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
33
32