LoginSignup
24
12

More than 1 year has passed since last update.

三項演算子 ( a ? b : c ) に true や false が出てきたら書き換えを検討しよう

Posted at

JavaScript の例ですが、Java や Ruby など、同じような三項演算子の文法をもっているものには同じように使えます。

(1)

BAD

const a = x === y ? true : false

GOOD

const a = x === y

(2)

NORMAL

const a = x === y ? true : null

BETTER

const a = x === y || null

(3)

NORMAL

const a = x === y ? f() : false

BETTER

const a = x === y && f()

(4)

NORMAL

const a = x === y ? false : f()

BETTER

const a = x !== y && f()
24
12
2

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
24
12