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 44907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:43:33+00:00 2026-05-10T15:43:33+00:00

What is the best way to find if an object is in an array?

  • 0

What is the best way to find if an object is in an array?

This is the best way I know:

function include(arr, obj) {   for (var i = 0; i < arr.length; i++) {     if (arr[i] == obj) return true;   } }  console.log(include([1, 2, 3, 4], 3)); // true console.log(include([1, 2, 3, 4], 6)); // undefined

  • 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. 2026-05-10T15:43:33+00:00Added an answer on May 10, 2026 at 3:43 pm

    As of ECMAScript 2016 you can use includes()

    arr.includes(obj); 

    If you want to support IE or other older browsers:

    function include(arr,obj) {     return (arr.indexOf(obj) != -1); } 

    EDIT: This will not work on IE6, 7 or 8 though. The best workaround is to define it yourself if it’s not present:

    1. Mozilla’s (ECMA-262) version:

         if (!Array.prototype.indexOf)    {          Array.prototype.indexOf = function(searchElement /*, fromIndex */)       {        "use strict";       if (this === void 0 || this === null)        throw new TypeError();       var t = Object(this);      var len = t.length >>> 0;      if (len === 0)        return -1;       var n = 0;      if (arguments.length > 0)      {        n = Number(arguments[1]);        if (n !== n)          n = 0;        else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))          n = (n > 0 || -1) * Math.floor(Math.abs(n));      }       if (n >= len)        return -1;       var k = n >= 0            ? n            : Math.max(len - Math.abs(n), 0);       for (; k < len; k++)      {        if (k in t && t[k] === searchElement)          return k;      }      return -1;    };   } 
    2. Daniel James‘s version:

       if (!Array.prototype.indexOf) {    Array.prototype.indexOf = function (obj, fromIndex) {      if (fromIndex == null) {          fromIndex = 0;      } else if (fromIndex < 0) {          fromIndex = Math.max(0, this.length + fromIndex);      }      for (var i = fromIndex, j = this.length; i < j; i++) {          if (this[i] === obj)              return i;      }      return -1;    };  } 
    3. roosteronacid‘s version:

       Array.prototype.hasObject = (    !Array.indexOf ? function (o)    {      var l = this.length + 1;      while (l -= 1)      {          if (this[l - 1] === o)          {              return true;          }      }      return false;    } : function (o)    {      return (this.indexOf(o) !== -1);    }  ); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to find something in a list? I know LINQ
What is the best way to find out if a primary key with a
What is the best way to find out whether two number ranges intersect? My
What is the best way to find the total number of words in a
What is the best way to find all the system fonts a user has
Using jQuery, what's the best way to find the next form element on the
I'm trying to find the best way to speed up the delivery of the
i am trying to find the best way to display results on my page
I am trying to find the best way to design the database in order
What's the best way of appending an array of jQuery elements to the page?

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.