DEV Community

Panayiotis Georgiou
Panayiotis Georgiou

Posted on

2

How to Check if a Value is Within a Range of Numbers in JavaScript

We can use the JavaScript’s greater than or equal to and less than or equal to operators to check if a number is in between 2 numbers.



const between = (x, min, max) => {
  return x >= min && x <= max;
}
// ...
const x = 0.002
if (between(x, 0.001, 0.009)) {
  // something
}


Enter fullscreen mode Exit fullscreen mode

We create the between function with the x , min and max parameters.

JS

x is the number we want to check if it’s between min and max .

Then we call it in the if statement to see if x is between 0.001 and 0.009.

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs