Number

_.inRange

Checks if n is between start and up to, but not including, end. If end is not specified, it's set to start with start then set to 0. If start is greater than end the params are swapped to support negative ranges.

  1. // Lodash
  2. _.inRange(3, 2, 4);
  3. // output: true
  4. _.inRange(-3, -2, -6);
  5. // output: true
  6.  
  7. //Native
  8. const inRange = (num, init, final) => {
  9. if(final === undefined){
  10. final = init;
  11. init = 0;
  12. }
  13. return (num >= Math.min(init, final) && num < Math.max(init, final));
  14. }
  15.  
  16. inRange(3, 2, 4);
  17. // output: true
  18. inRange(-3, -2, -6);
  19. // output: true

Browser Support for Math.min() and Math.max()

ChromeEdgeFirefoxIEOperaSafari