LoginSignup
0
0

More than 1 year has passed since last update.

【JavaScript】論理演算子のtruthyな値、falsyな値のまとめ

Last updated at Posted at 2023-01-08

falsyな値とは

真偽値に変換した場合にfalse(偽)とみなされる値のこと

truthyな値とは

falsy以外の値のこと(trueになる値)

falsyになる値

false
null
0(数字)
""(空文字)
undefined
Nan
0n (big int)

論理積 (&&)

論理積は左側からfalsyな値を探して、見つかったら変数等に格納します
falsyな値がなければ一番右側の値が格納されます


//result1には""が格納される
const result1 = "" && "aaa";

//result2には0が格納される
const result2 = 5 && 1 && 0 && 10;

//result3には2が格納される
const result3 = "bbb" && 2; 

論理和 (||)

論理和はtruthyな値を見つけたら変数等に格納します
truthyな値が複数ある場合は左側の値を変数等に格納します


//result4には"aaa"が格納される
const result4 = "" || "aaa";

//result5には5が格納される
const result5 = null || 5 || undefined;

//result6には4が格納される
const result6 =  4 || 6; 
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