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?

【React初心者メモ】===と==の違い

Last updated at Posted at 2025-05-05

JavaやPythonなどの他言語では、条件分岐の等価演算子として==をよく使います。
しかし、Reactでは===をよく見かけます。違いは何なのでしょうか?

==

値だけ比較する(型が違っても比較する)

'1' == 1     // true:型が違っても値が同じとみなされる
false == 0   // true:型変換で一致する
null == undefined   // true(特例)

===

型も値も比較する(型が違えば false)

'1' === 1    // false:型が違うので一致しない
false === 0  // false:型が違う
null === undefined  // false

結論

==:値のみの比較(抽象的等価)
===:型も値も比較(厳密等価)

== は自動的に型を変換して比較するため、思わぬバグが発生しやすいです。
こういった予測できない動作を避けるために、
現代のJavaScriptでは常に ===を使うのがベストプラクティスとなっています。

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?