0
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 1 year has passed since last update.

【JavaScript】falsyな値

Posted at

JavaScriptにおいてfalseと扱われる(falsyな)値には以下が存在する。

説明
false true、falseのfalseキーワード
0 数値0
-0 数値-0
0n BigInt値0
"" 空文字列
null 何も存在しないこと
undefined 宣言のみされた変数などの未定義値
NaN どの数値とも一致しない特性を持つNumber型の値
if (false) {
  console.log('trueです');    
} else {
  console.log('falseです');    
}
// => falseです

if (0) {
  console.log('trueです');    
} else {
  console.log('falseです'); 
}
// => falseです

if (-0) {
  console.log('trueです');    
} else {
  console.log('falseです');    
}
// => falseです

if (0n) {
  console.log('trueです');    
} else {
  console.log('falseです');    
}
// => falseです

if ("") {
  console.log('trueです');    
} else {
  console.log('falseです');    
}
// => falseです

if (null) {
  console.log('trueです');    
} else {
  console.log('falseです');    
}
// => falseです

if (undefined) {
  console.log('trueです');    
} else {
  console.log('falseです');    
}
// => falseです

if (NaN) {
  console.log('trueです');    
} else {
  console.log('falseです');    
}
// => falseです
0
0
0

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