2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Javascript ES6の真偽判定

Last updated at Posted at 2020-05-09

JavaScriptにおける真偽判定がややこしすぎたので表にまとめてみました。

Trueになるやつ

中身
Number 0とNaN以外
Array []
Object {}
String 'a'

falseになるやつ

中身
Object null
Number 0
Number NaN
Number [].length
Number Object.keys({}).length
String ''(空文字)
Number ''.length

推察(この推察 = trueなのかは知らない)

Number

0ないしNaNならfalse
それ以外はtrue

Object,Array

基本的にtrueになっちゃう
-> lengthでNumberにしようね
(Objectに関してはObject.keysしないといけないから他にもっと楽な方法はあるかもしれない)

String

[]や{}を差し置いて空文字は生意気にもfalse
それ以外はtrue

null

おまえは流石にfalse

#まとめ
lengthはっょぃ

※コメントにて間違いをご指摘くださった@htsign様、誠にありがとうございましたm(_ _)m

2
0
3

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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?