• 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 / Get the index of the first matching item in an array

Get the index of the first matching item in an array

Get the index of the first matching item in an array

Get the index of the first matching item in an array. Returns -1 if no matching item is found.

var sandwiches = ['turkey', 'tuna', 'blt', 'pb&jb'];

// returns 1
sandwiches.indexOf('tuna');

// returns -1
sandwiches.indexOf('ham');

Source

https://vanillajstoolkit.com/reference/arrays/array-indexof.md/

Miscellaneous

Related Snippets:

  • Take the content of an array and return a single value
  • Insert an element before another one
  • Return all the elements of an array except the last one
  • Drop list elements from the right

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers