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?

真偽値と比較演算子(2)

Last updated at Posted at 2025-03-15

等価演算子

比較演算子には、左と右の値が等しいかを調べる演算子も存在します。
a == bはaとbが等しければtrue、等しくなければfalseになります。 a != bはその逆です。

image.png

等価演算子の注意点
数値「12」と文字列「"12"」は異なるデータですが、等価演算子では同じデータとして扱うよう変換されます。
これは都合の良い時もありますが、いつもそうとは限らないので注意が必要です。

厳密等価演算子

厳密等価演算子を使うと、文字列「"12"」と数値「12」を異なるデータとして扱うことができます。
特に理由がなければ、厳密に比較できる厳密等価演算子を利用するようにしましょう。

image.png

例題

qiita.js
if文を使い以下の例題に取り組んでみましょう

const password = "hello world";

//passwordの値が"hello world"の場合、「ログインに成功しました」と出力してください

//passwordの値が"hello world"でない場合、「パスワードが間違っています」と出力してください
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?