mirror of
https://github.com/Fechin/reference.git
synced 2026-11-26 07:33:24 +03:00
761 B
761 B
title, tags, layout
| title | tags | layout | |||||
|---|---|---|---|---|---|---|---|
| How to check if a number is in a given range in JavaScript |
|
note |
In this Article we will go through how to check if a number is in a given range only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.
Let's define this short function:
const inRange = (num, a, b, threshold = 0) => (Math.min(a, b) - threshold <= num && num <= Math.max(a, b) + threshold);
Example
inRange(10, 5, 15); // true
inRange(10, 5, 6); // false
inRange(10, 15, 5); // true
inRange(-10, -5, -15); // true