0
1

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.

論理積演算子と論理和演算子について

Posted at

論理積演算子と論理和演算子について

概要

論理積演算子と論理和演算子の備忘録

アンド演算子(論理積演算子)

基本

&&の両方がtrueの場合にのみtrueを返す

let hoge;
hoge = true && false;
// false

hoge = false && false;
// false

hoge = true && true;
// true

応用(厳密な定義)

左側がtruthyだったら、右側の値を返す
左側がfalsy だったら、左側の値を返す

hoge = 'hello' && 'hi';
// hi

hoge = true && 'hi';
// hi

hoge = false && 'hi';
// false

hoge = 0 && 'hi';
// 0

パイプ演算子(論理和演算子)

基本

両方がfalseの場合にのみfalseを返す

let hoge;
hoge = true || false;
// hoge = false || true;
// true

hoge = false || false;
// false

応用(厳密な定義)

左側がtruthyだったら、左側の値を返す
左側がfalsy だったら、右側の値を返す

let hoge;
hoge = 'hello' || 'hi';
// hello

hoge = '' || 'hi';
// hi
e.g.
const userInput = '';
const userName = userInput || 'User';
// userInputが空文字だったら、UserをuserNameに代入する.
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?