• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / Miscellaneous / Check if an Item Exists in an Array

Check if an Item Exists in an Array

Check if an Item Exists in an Array

Instead of using the indexOf() method to check if an element is in the array, you can use the includes() method. This makes your intent very clear:

let numbers = [1, 2, 3];

// LONGER FORM
const hasNumber1 = numbers.indexOf(1) > -1 // -> True

// SHORTHAND/CLEANER APPROACH
const hasNumber1 = numbers.includes(1)     // -> True

Source

https://medium.com/geekculture/20-javascript-snippets-to-code-like-a-pro-86f5fda5598e

Miscellaneous

Related Snippets:

  • Insert an element into the DOM after another one
  • Get an attribute on an element
  • Return all elements in an array except for the first one
  • Reverse the order of items in an array

Primary Sidebar

JSON Compare

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2026 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers