October 19, 2020

Comparing to multiple values

Thanks to everyone who responded to my inquiry about potential source control content. Your help and insight ensures I’m able to offer useful, valuable content.

Here’s a really common mistake I see a lot of newer JavaScript/SuiteScript developers making. It’s quite often that you’ll need to check whether a field value matches one of several possible values that you care about.

Let’s say you want to do something important for every Customer with a first name of Tom, Aryn, Lovell, or Jaron. Here’s the first attempt I often see for that sort of comparison:


While logically this might seem reasonable, it shows a misunderstanding of how the || operator works. The || operator IS NOT how you create lists of values in JavaScript. You’re actually looking for an Array.

Arrays create lists of values and provide methods for asking “Is a specific value within the Array?”. The first step is to store our list of possible values as an Array, not as a boolean expression like you see above.

Next, we can use an Array method to determine whether our value is contained within the Array. In SuiteScript 2.0 (which is ECMAScript version 5.1), we can use the indexOf method. As its name might imply, this method returns the index of a given value if it occurs within the Array; if the value does not occur within the Array, then it returns -1. So in SuiteScript 2.0, we can do something like:


In SuiteScript 2.1 (which is ECMAScript 2019), we can use the includes method. This is a little cleaner as it removes the need to compare against a number. The method returns true if the given value occurs in the Array and false if the value does not occur within the Array. In SuiteScript 2.1, we can do something like:


As an added bonus, since the only thing the if..else branches do is return the corresponding boolean value, you can reduce to simply returning the comparison result:


If you’d like to improve your logical deduction and reduction capabilities, Boolean Algebra is a worthwhile area of study – as if you needed more homework.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Related posts

March 3, 2021

Does the physical location of a competent employee change the amount of value they can deliver for your organization? If it does, I’d be keen to hear your story. If it does not, why ...

Read More

March 2, 2021

Today a friend and former colleague of mine – who happens to now be in a development leadership role for a NetSuite partner – was lamenting the extreme difficulty of finding and hiring NetSuite ...

Read More

March 1, 2021

Software is never finished; clients are always changing and re-prioritizing. NetSuite is ever a moving target – always updating and evolving. Market forces are always shifting and shoving, forcing new demands on your business. ...

Read More