18
8

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 5 years have passed since last update.

javascriptのif文で2つ以上の数値を比較する(範囲指定、条件分岐)

Last updated at Posted at 2018-11-09

TL;DR

  • javascriptで2つ以上の数値を使って範囲を示す条件式を書く場合、「&&」で連結する必要がある
var a = 3;

if (1 <= a && a < 5){
  //処理
}

自戒

  • javascriptを普段あまり使わないので、下記のように書いてしまっていた。
  • 「条件式あってるはずなのになんで思った通りの処理ができないんだ???」って1時間くらい悩んだので、自戒の念を込めてメモしておく。
var b = 5;

if (1 <= b < 5){
  // 処理A
} else if (5 <= b < 10) {
  // 処理B
}
18
8
3

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
18
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?