Searching Arrays

  • This lesson covers methods used to search elements in arrays.
  • Introduction to Searching in Arrays

    In real-world programming, data is often stored in arrays.
    Once data is stored, the most common requirement is to search for specific values.

    Examples:

    • Checking if a product exists in a cart

    • Finding a user by ID

    • Searching for a number in a list

    • Verifying if a value is present before performing an action

    JavaScript provides built-in array methods to make searching easy and efficient.

    In this lesson, we will learn:

    1. find()

    2. includes()

    3. indexOf()

    Why Searching Arrays is Important

    Without search methods:

    • Manual loops are required

    • Code becomes lengthy

    • Logic becomes repetitive

    With array search methods:

    • Code is shorter

    • Readability improves

    • Logic is clearer

    • Performance is better

    find() Method

    What is find() ?

    The find() method is used to search for an element in an array based on a condition.

    It returns:

    • The first element that satisfies the condition

    • undefined if no element is found

      Syntax of find()

      array.find(function(element) {

        return condition;

      });

      Or using arrow function:

      array.find(element => condition);

      Important Points About find()

      • It returns the actual element

      • It stops searching after finding the first match

      • It works well with objects

      • It does not change the original array

    find Method in Arrays

    Returns the first element that matches a given condition.

    // Find number greater than 10
    
    let numbers = [4, 9, 12, 15, 3];
    
    let result = numbers.find(num => num > 10);
    
    console.log(result); // 12
    
    
    // Find even number
    
    let nums = [3, 7, 9, 8, 10];
    
    let even = nums.find(n => n % 2 === 0);
    
    console.log(even); // 8
    
    
    // Find object in array
    
    let students = [
      { id: 1, name: "Rahul" },
      { id: 2, name: "Amit" },
      { id: 3, name: "Neha" }
    ];
    
    let student = students.find(s => s.id === 2);
    
    console.log(student); // { id: 2, name: "Amit" }
    • When to Use find()

      • When searching based on a condition

      • When working with objects

      • When you need the actual element

        includes() Method

        What is includes() ?

        The includes() method checks whether an array contains a specific value.

        It returns:

        • true if value exists

        • false if value does not exist

          Syntax of includes()

          array.includes(value);

          Important Points About includes()

          • Returns only true or false

          • Simple and fast

          • Best for basic value checking

          • Does not return index or element

        includes Method in Arrays

        Checks whether a specific value exists inside an array.

        // Check if value exists
        
        let fruits = ["apple", "banana", "mango"];
        
        console.log(fruits.includes("banana")); // true
        
        
        // Value not found
        
        console.log(fruits.includes("orange")); // false
        
        
        // Using includes in condition
        
        let cart = ["mobile", "laptop", "earphones"];
        
        if (cart.includes("laptop")) {
          console.log("Item already in cart");
        } else {
          console.log("Item not found");
        }
        • When to Use includes()

          • When you only need yes/no answer

          • When checking simple values

          • When validating input

            indexOf() Method

            What is indexOf() ?

            The indexOf() method searches for a value and returns:

            • The index position of the first match

            • -1 if the value is not found

            Syntax of indexOf()

            array.indexOf(value);

            Important Points About indexOf()

            • Returns index number

            • Returns -1 if not found

            • Works with primitive values

            • Case-sensitive for strings

          indexOf Method in Arrays

          Returns the position of an element or -1 if it is not found.

          // Find index of element
          
          let colors = ["red", "green", "blue"];
          
          let index = colors.indexOf("green");
          
          console.log(index); // 1
          
          
          // Element not found
          
          console.log(colors.indexOf("yellow")); // -1
          
          
          // Using indexOf in condition
          
          let subjects = ["Math", "Science", "English"];
          
          if (subjects.indexOf("Science") !== -1) {
            console.log("Subject found");
          } else {
            console.log("Subject not found");
          }
          • find() vs includes() vs indexOf()
            Featurefind()includes()indexOf()
            Return valueElementtrue / falseIndex / -1
            Based on conditionYesNoNo
            Works with objectsYesNoNo
            Stops at first matchYesYesYes
            Use caseComplex searchSimple checkIndex lookup

            Common Mistakes

            • Using includes() to get index

            • Using indexOf() with objects

            • Forgetting -1 check in indexOf()

            • Expecting find() to return multiple values

            Best Practices

            • Use find() for object-based searching

            • Use includes() for quick existence checks

            • Use indexOf() when index position is needed

            • Prefer readability over complex logic

            Real-Life Use Cases

            • Login validation

            • Product availability check

            • Student record search

            • Feature access control