Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 692403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:38:50+00:00 2026-05-14T02:38:50+00:00

In SQL we can see if a string is in a list like so:

  • 0

In SQL we can see if a string is in a list like so:

Column IN ('a', 'b', 'c')

What’s a good way to do this in JavaScript? It’s so clunky to do this:

if (expression1 || expression2 || str === 'a' || str === 'b' || str === 'c') {
   // do something
}

And I’m not sure about the performance or clarity of this:

if (expression1 || expression2 || {a:1, b:1, c:1}[str]) {
   // do something
}

Or one could use the switch function:

var str = 'a',
   flag = false;

switch (str) {
   case 'a':
   case 'b':
   case 'c':
      flag = true;
   default:
}

if (expression1 || expression2 || flag) {
   // do something
}

But that is a horrible mess. Any ideas?

In this case, I have to use Internet Explorer 7 as it’s for a corporate intranet page. So ['a', 'b', 'c'].indexOf(str) !== -1 won’t work natively without some syntax sugar.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:38:51+00:00Added an answer on May 14, 2026 at 2:38 am

    ES6 (ES2015) and up

    If you’re using ECMAScript 6 (a.k.a. ES2015) or higher, the cleanest way is to construct an array of the items and use Array.includes:

    ['a', 'b', 'c'].includes('b')
    

    This has some inherent benefits over indexOf because it can properly test for the presence of NaN in the list, and can match missing array elements such as the middle one in [1, , 2] to undefined. It also treats +0 and -0 as equal. includes also works on JavaScript typed arrays such as Uint8Array.

    If you’re concerned about browser support (such as for IE or Edge), you can check Array.includes at CanIUse.Com, and if you want to target a browser or browser version that’s missing includes, you’ll need to transpile to a lower ECMAScript version using a tool such as Babel, or include a polyfill script in the browser, such as those available at polyfill.io.

    Higher Performance

    Note that there is no guarantee that Array.includes() execution time won’t scale with the number of elements in the array: it can have performance O(n). If you need higher performance, and won’t be constructing the set of items repeatedly (but will be repeatedly checking if the items contain some element), you should use a Set because the ES spec requires implementations of Set (and Map as well) to be sub-linear for reads:

    The specification requires sets to be implemented "that, on average, provide access times that are sublinear on the number of elements in the collection". Therefore, it could be represented internally as a hash table (with O(1) lookup), a search tree (with O(log(N)) lookup), or any other data structure, as long as the complexity is better than O(N).

    const interestingItems = new Set(['a', 'b', 'c'])
    const isItemInSet = interestingItems.has('b')
    

    Note that you can pass in any iterable item to the Set constructor (anything that supports for…of). You can also convert a Set to an array using Array.from(set) or by spreading it [...set].

    Without An Array

    This is not really recommended, but you could add a new isInList property to strings as follows:

    if (!String.prototype.isInList) {
      Object.defineProperty(String.prototype, 'isInList', {
        get: () => function(...args) {
          let value = this.valueOf();
          for (let i = 0, l = args.length; i < l; i += 1) {
            if (arguments[i] === value) return true;
          }
          return false;
        }
      });
    }
    

    Then use it like so:

    'fox'.isInList('weasel', 'fox', 'stoat') // true
    'fox'.isInList('weasel', 'stoat') // false
    

    You can do the same thing for Number.prototype.

    Note that Object.defineProperty cannot be used in IE8 and earlier, or very old versions of other browsers. However, it is a far superior solution to String.prototype.isInList = function() { ... } because using simple assignment like that will create an enumerable property on String.prototype, which is more likely to break code.

    Array.indexOf

    If you are using a modern browser, indexOf always works. However, for IE8 and earlier you’ll need a polyfill.

    If indexOf returns -1, the item is not in the list. Be mindful though, that this method will not properly check for NaN, and while it can match an explicit undefined, it can’t match a missing element to undefined as in the array [1, , 2].

    Polyfill for indexOf or includes in IE, or any other browser/version lacking support

    If you don’t want to use a service like polyfill.io as mentioned above, you can always include in your own source code standards-compliant custom polyfills. For example, the CoreJs library has an implementation of indexOf.

    In this situation where I had to make a solution for Internet Explorer 7, I "rolled my own" simpler version of the indexOf() function that is not standards-compliant:

    if (!Array.prototype.indexOf) {
       Array.prototype.indexOf = function(item) {
          var i = this.length;
          while (i--) {
             if (this[i] === item) return i;
          }
          return -1;
       }
    }
    

    Notes On Modifying Object Prototypes

    However, I don’t think modifying String.prototype or Array.prototype is a good strategy long term. Modifying object prototypes in JavaScript can lead to serious bugs. You need to decide whether doing so is safe in your own environment. Of primary note is that iterating an array (when Array.prototype has added properties) with for ... in will return the new function name as one of the keys:

    Array.prototype.blah = function() { console.log('blah'); };
    let arr = [1, 2, 3];
    for (let x in arr) { console.log(x); }
    // Result:
    0
    1
    2
    blah // Extra member iterated over!
    

    Your code may work now, but the moment someone in the future adds a third-party JavaScript library or plugin that isn’t zealously guarding against inherited keys, everything can break.

    The old way to avoid that breakage is, during enumeration, to check each value to see if the object actually has it as a non-inherited property with if (arr.hasOwnProperty(x)) and only then work with that x.

    The new ES6 ways to avoid this extra-key problem are:

    1. Use of instead of in, for (let x of arr). However, depending on the output target and the exact settings/capabilities of your down-leveling transpiler, this may not be reliable. Plus, unless you can guarantee that all of your code and third-party libraries strictly stick to this method, then for the purposes of this question you’ll probably just want to use includes as stated above.

    2. Define your new properties on the prototype using Object.defineProperty(), as this will make the property (by default) non-enumerable. This only truly solves the problem if all the JavaScript libraries or modules you use also do this.

    Be Aware of One Last Issue

    Last, be aware that while polyfills make sense, and modifying object prototypes is a useful strategy, there can occasionally still be scoping problems with that approach.

    In a browser, each distinct document object is its own new global scope, and in browser JS it is possible to create new documents (such as those used for off-screen rendering or to create document fragments) or to get a reference to another page’s document object (such as via inter-page communication using a named-target link) so it’s possible in certain (rare?) circumstances that object prototypes won’t have the methods you expect them to have—though you could always run your polyfills again against the new global objects…

    In Node.js, modifying prototypes of global objects may be safe, but modifying the prototypes of non-global, imported objects could lead to breakage if you ever end up with two versions of the same package being required/imported, because imports of the two versions will not expose the same objects, thus won’t have the same object prototypes. That is, your code could work fine until a dependency or sub-dependency uses a different version from the one you expect, and without any of your own code changing, a simple npm install or yarn install could trigger this problem. (There are options to deal with this, such as yarn’s resolutions property in the package.json, but that’s not a good thing to rely on if you have other options.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 347k
  • Answers 347k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No. The callee (the target function) is generally not responsible… May 14, 2026 at 6:20 am
  • Editorial Team
    Editorial Team added an answer I've solved it. I had a non-pooled datasource: <bean name="dataSource"… May 14, 2026 at 6:20 am
  • Editorial Team
    Editorial Team added an answer Add an output parameter to your procedure: @new_id int output… May 14, 2026 at 6:20 am

Related Questions

In a .NET project I need to verify if a string is a valid
I am getting deadlocks occasionally in sql server. I created a function for locking
We've been doing some testing with ColdFusion and MySQL and are a bit puzzled.
We have a Delphi 7 application that runs as an ISAPI extension in IIS6.
---AFTER FURTHER INVESTIGATION--- tblABC in the below example must be a linked table (to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.