Skip to content
the poor coder

if between two numbers javascript

Poor Man
Published
Reading time
2 min read
On this page

If Between Two Numbers Javascript#

Have you ever wondered how to check if a number is between two other numbers in JavaScript? It's a common problem that developers come across when working with numerical data.

Method 1: Using Logical Operators#

One way to solve this problem is to use logical operators. The logical operators we can use for this are the greater than (>) and less than (<) operators.

Let's say we have two numbers, num1 and num2. We want to check if a third number, num3, is between num1 and num2.

Text

let num1 = 5;
let num2 = 10;
let num3 = 7;

if (num3 > num1 && num3 < num2) {
   console.log(num3 + " is between " + num1 + " and " + num2);
} else {
   console.log(num3 + " is not between " + num1 + " and " + num2);
}

In this example, we use the logical AND operator (&&) to check if num3 is greater than num1 AND less than num2. If both conditions are true, then we know that num3 is between num1 and num2.

Method 2: Using Math Functions#

An alternative method is to use math functions, specifically the Math.min() and Math.max() functions. The Math.min() function returns the smallest of zero or more numbers, while the Math.max() function returns the largest of zero or more numbers.

Text

let num1 = 5;
let num2 = 10;
let num3 = 7;

if (num3 > Math.min(num1, num2) && num3 < Math.max(num1, num2)) {
   console.log(num3 + " is between " + num1 + " and " + num2);
} else {
   console.log(num3 + " is not between " + num1 + " and " + num2);
}

Here, we use the Math.min() function to get the smallest number between num1 and num2, and the Math.max() function to get the largest number between num1 and num2. We then compare num3 with these values to see if it is between them.

Method 3: Using Ternary Operator#

Another way to solve this problem is to use a ternary operator.

Text

let num1 = 5;
let num2 = 10;
let num3 = 7;

let result = (num3 > num1 && num3 < num2) ? (num3 + " is between " + num1 + " and " + num2) : (num3 + " is not between " + num1 + " and " + num2);

console.log(result);

In this example, we use the ternary operator to check if num3 is between num1 and num2. If it is, we return a message saying so. If it isn't, we return a message saying that it's not.

These are just a few ways to check if a number is between two other numbers in JavaScript. Depending on your use case, one method may be more appropriate than another.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC