Home > InDesign Scripts > QuickTip: Check if a number is between two numbers

QuickTip: Check if a number is between two numbers

In JavaScript I always miss one great feature from MySQL and that is operator between. It checks if a number is between two numbers you enter. So I decided to create small ‘Number’ prototype to achieve this. If a number is between is between then it returns true, and if not returns false. It’s perfect for using in ‘if()’ statements.

Number.prototype.between = function(first,last){
    return (first < last ? this >= first && this <= last : this >= last && this <= first);
}

Usage:

var myNum = 315;

if(myNum.between(300,320)){ //returns true
  //if true
}else{
  //if false
}

Have fun! 😀

Categories: InDesign Scripts Tags:
  1. zF
    February 10, 2011 at 13:44

    Yeah, try this code to check if -5 is between 0 and -10 with this code. You’d be surprised 🙂

  2. yo
    January 3, 2012 at 16:27

    hi, this one really helped me, thanks!

  1. No trackbacks yet.

Leave a comment