0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JS基礎文法~if文~

Posted at

概要

if文での条件分岐についての記事です。
if文とは、入力の値によって処理を変えて適切な結果を表示します。
プログラムでは、条件分岐の構文を使って処理の流れを変えます。

if文

index.js
let score = 50;
const num = 80;

if (score > num) {
   console.log("すごい!");
}else{
   console.log("頑張りましょう");
} 

基本的なif文は以下のようになります。
定数numの80よりもscoreがtrueの処理とfalseの処理を記述します。

index.js
score > num ? console.log("すごい!") :  console.log("頑張りましょう");

短縮すると上記のようになります。

index.js
 let score = 10;

 if (score < 60) {
   console.log("頑張りましょう");
 }else if(score > 60 ){
    console.log("さすが");
 }else {
    console.log("前進あるのみ");
 }

上記のプログラムだと数字以外が変数に代入された場合に前進あるのみと処理されます。

論理演算子

index.js
let name = "";
const job = "医者"

if(name === "なお" && job === "医者" ) {
  console.log("ご本人です");
} else {
  console.log("他人です");
}

A && B ▶両方がtrueであればtrue
A || B ▶どちらか一方がtrueであればtrue

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?