Skip to content

Instantly share code, notes, and snippets.

@dandean
Created August 5, 2009 04:54
Show Gist options
  • Save dandean/162507 to your computer and use it in GitHub Desktop.
Save dandean/162507 to your computer and use it in GitHub Desktop.
Number.prototype.isBetween implementation...
/**
* Number#between(low, high[, inclusive = false]) -> boolean
* - low(Number): Number must be greater than `low` value
* - high(Number): Number must be less than `high` value
* - inclusive(Boolean): [Optional] Include low and high numbers in the check.
* Defaults to `false`.
*
* Checks if the Number is between `low` and `high` values. Returns `true` or `false`
**/
Number.prototype.isBetween = function(low, high, inclusive) {
if (inclusive) {
return this >= low && this <= high;
}
return this > low && this < high;
}
var five = 5;
console.log(five.isBetween(3, 10)); // true
console.log(five.isBetween(9, 10)); // false
console.log(five.isBetween(5, 10)); // false
console.log(five.isBetween(5, 10, true)); // true
console.log(Number.MIN_VALUE.isBetween(0, 0.1)); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment