1

How following if condition true for ids=1

var ids = 1;

if (9 <= ids <= 14) {

  console.log(9 <= ids <= 14)

}

I want it to be true only if the value of ids between 9 and 14. Appreciate any help understanding how JavaScript operators work when checking range.

Nisal Edu
  • 7,237
  • 4
  • 28
  • 34

2 Answers2

5

You can't chain comparison operators like that. You have to do each comparison separately and use AND (&&) or OR (||).

var ids = 1;
if (9 <= ids && ids <= 14) {}
ATOMP
  • 1,311
  • 10
  • 29
3

You need to check with two conditions and one logical AND, because the first condition returns a boolean value and this is compared with the other value.

var ids = 1;

if (9 <= ids && ids <= 14) {
    console.log('in range');
} else {
    console.log('not in range');
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392