Check data using logical operators (===, <, >, &&, ||)

Check if data is greater than, less than, equal to, within a range and other logical operators

JavaScript allows you to perform various logic checks on data. A few of the more common checks are listed below.

Assuming you had a variable data with an unknown quantity you could check if it was a number equal to 5 with the following code.

JavaScript

if (data === 5) {
  // code here would be run if data is equal to 5
} else {
  // code here would run if data is not equal to 5
}

If you want to check if data is greater than or less than a value you can use the > or < operators.

JavaScript

if (data > 3) {
 // code here would be run if data is greater than 3
} else {
  // code here would run if data is less than or equal to 3
}

If you want to check if data is less than or equal to a value you can use <= . You can also use >= for greater than or equal to.

JavaScript

if (data <= 8) {
  // code here would run if data is less than or equal to 8
}

If you want to check if data is in a range of values then we can use the AND operator (denoted with && ).

JavaScript

if (data > 0 && data < 10) {
  // data is greater than 0 and less than 10
}

If you want to check if data satisfies one of two or more criteria we can use the OR operator (denoted with || ).

JavaScript

if (data === 3 || data === 5) {
  // code here will run if data is equal to 3 or 5
}