LoginSignup
155
100

More than 5 years have passed since last update.

[JavaScript]null または空文字判定

Posted at

自戒メモ

結論

勝手に判定してくれる
(JavaScript では null ないし空文字を条件式において false と判定する)

コード

やってたこと:
 文字列が入ってくることを想定している変数 str について null か空文字なら何か処理をいれたかった

// Java のライブラリとかである String.isEmpty みたいなことをやってた
if (str == null || str == '') {
  // do something
}

こうすればよい

// null か空文字なら false と判定してくれる
if (!str) {
  // do something
}
155
100
1

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
155
100