JS-152-Objects-Arrays
  • Introduction
  • 04-Objects
    • OOP-Basics
      • Overview
      • Coding the Example
    • Prototypes
      • Object Prototypes
      • Prototypes Continued
    • Inheritance
      • Inheritance
    • Synopsis
      • Synopsis
    • Challenges
    • Solutions
      • Constructor Function
        • Person Constructor
        • Explanation
      • Inheritance Challenge
        • Code
        • Explanation
      • Family Challenge
        • Child
        • Pet
  • 05-Arrays
    • Array Basics
      • Array Overview
      • Creating an Array
      • Individual Elements and Length
      • Iterating Over Arrays
    • Array Methods
      • Methods Overview
      • .join() Method
      • .reverse() Method
      • .split() Method
      • .replace() Method
      • .splice() Method
      • .map() Method
      • .indexOf() Method
      • .filter() Method
      • .every() Method
    • Palindrome Algorithm
    • Array Challenges
    • Solutions
      • First and Last
        • First Element
        • Last Element
      • Most Frequent
      • Largest and Smallest
        • Single Array
        • Multiple Arrays
      • Missing Number
      • "Arrays" in the DOM
      • Sorting
        • Bubble Sort
        • Selection Sort
    • Resources
Powered by GitBook
On this page
  1. 05-Arrays
  2. Array Methods

.indexOf() Method

The .indexOf() method can be used to determine where an element is located in the array. Duplicates give the index of the first instance. Let's use the letters of the alphabet and figure out what letter q is.

var alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";

var alphaQ = alphabet.split(" ").indexOf("q");

alphaQ += 1;
console.log(alphaQ);

You should have gotten 17. Let's break this down:

  • I put the .split() and .indexOf methods together to show you that you can combine methods, and also just to save time and space.

  • I added one to alphaQ before logging it to the console because arrays are zero-indexed.

Previous.map() MethodNext.filter() Method

Last updated 7 years ago