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?

複雑な条件文の効率的な書き方

Posted at

複雑な条件文を書くときに括弧でまとめると便利なので、メモしておきます。

こんな変数があったと仮定

const conditionA = true
const conditionB = true
const conditionC = false

ケース1 Not A & Not B

const res1 = !conditionA && !conditionB
// ↓と括弧でまとめられる
const res2 = !(conditionA && conditionB)
// 結果はfalse

ケース2 Not A or Not B

const res3 = !conditionA || !conditionB
// ↓と括弧でまとめられる
const res4 = !(conditionA || conditionB)
// 結果はfalse

ケース3 A or B & C

const res5 = conditionA || (conditionB && conditionC)

ケース4 A or Not B & Not C

const res6 = conditionA || !(conditionB && conditionC)

ケース5 A & B or C

const res7 = conditionA && conditionB || conditionA && conditionC
// ↓の方が見やすい
const res8 = conditionA && (conditionB || conditionC)
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?