Return correct value from recursive indexOf in JavaScript?

A computer pattern or idea called recursion is present in many programming languages, including JavaScript. It is a feature used to build a function that repeatedly calls itself, but with a smaller input each time, until the intended outcome of the code is realized.

In this article, we will return the correct value from a recursive indexOf() function in JavaScript. Let's dive into the article for a better understanding.

Understanding Recursive indexOf Implementation

The position of the first occurrence of a value in a string or array is returned by the indexOf() method. -1 is returned by the indexOf() method if the value cannot be found. The indexOf() is case sensitive.

Syntax

Following is the syntax for indexOf() ?

indexOf(searchElement)
indexOf(searchElement, fromIndex)

Method 1: Recursive Search with Array Slicing

In the following example, we implement a recursive indexOf function using array slicing. We search for the index of element "c".

<!DOCTYPE html>
<html>
   <body>
      <script>
         function search(array, value) {
            if (array[0] === value) {
               return 0;
            }
            if (array.length === 0) {
               return -1;
            }
            const result = search(array.slice(1), value);
            return result === -1 ? -1 : result + 1;
         }
         document.write("The indexOf c was: " +
         search(["a", "b", "c", "d"], "c"));
      </script>
   </body>
</html>
The indexOf c was: 2

Method 2: Recursive Search with Index Parameter

This approach uses an index parameter to track the current position, making it more efficient than array slicing.

<!DOCTYPE html>
<html>
   <body>
      <p id="tutorial"></p>
      <script>
         function search(arr, value, i = 0) {
            if (i >= arr.length) return -1;
            if (arr[i] === value) return i;
            return search(arr, value, i + 1);
         }
         let array = ["Raj", "Kamal", "Vikram", "Ravi"];
         document.getElementById("tutorial").innerHTML = "The indexOf Vespa was: " + (search(array, "Vespa"));
      </script>
   </body>
</html>
The indexOf Vespa was: -1

The output shows "-1" because the value "Vespa" was not found in the array.

Method 3: Built-in indexOf Method

For comparison, here's how the built-in indexOf method works:

<!DOCTYPE html>
<html>
   <body>
      <p id="tutorial"></p>
      <script>
         const array = ['Bheem', 'Nayak', 'Ravi', 'Dora'];
         const result = array.indexOf('Ravi');
         document.getElementById("tutorial").innerHTML = "The indexOf() of required value was: " + result;
      </script>
   </body>
</html>
The indexOf() of required value was: 2

Comparison

Method Efficiency Memory Usage Use Case
Array Slicing Lower Higher Educational purposes
Index Parameter Better Lower Recursive implementation
Built-in indexOf Best Optimal Production code

Key Points

  • Recursive indexOf functions demonstrate recursion concepts but are less efficient than built-in methods
  • The index parameter approach is more memory-efficient than array slicing
  • Both recursive methods return -1 when the element is not found, matching built-in behavior

Conclusion

Recursive indexOf implementations help understand recursion principles. While educational, use built-in indexOf() for production code as it's optimized and more efficient.

Updated on: 2026-03-15T23:19:00+05:30

429 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements