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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: