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初心者メモ】if文

Last updated at Posted at 2025-06-07

if文の基本

React(JavaScript)では、条件分岐ifの値がtruthyかfalsyかによって、条件が成り立つかが自動で決まります。

falsy:false, 0, -0, 0n, "", null, undefined, NaN
truthy:上記以外の全ての値

elseとelse if

複数の条件を扱いたい場合は、elseやelse if文を使います。

if (条件1) {
  // 条件1がtrueの時の処理
} else if (条件2) {
  // 条件2がtrueの時の処理
} else {
  // どちらもfalseの時の処理
}

条件式

条件式を書きたい場合は、比較演算子や論理演算子を使って書きます。

比較演算子

>:より大きい
<:より小さい
>=:以上
<=:以下
===:等しい(型も)
!==:等しくない(型も)
==:等しい(型は無視)
!=:等しくない(型無視)

論理演算子

&&:AND(かつ)

let age = 20;
let isMember = true;
if (age >= 18 && isMember) {
  console.log("会員で18歳以上です");
}

||:OR(または)

let username = "";
let email = "user@example.com";
if (username || email) {
  console.log("ユーザー名またはメールアドレスが入力されています");
}

!;否定

let input = "";
if (!input) {
  console.log("入力が空です"); // inputが空文字(falsy)なので true になる
}
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?