- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return correct value from recursive indexOf in JavaScript?
You can create your own function. If the search value is found then the index is returned otherwise return -1.
Example
Following is the code −
const indexOf = (arrayValues, v, index = 0) => index >= arrayValues.length ? -1 : arrayValues[index] === v ? index : indexOf(arrayValues, v, index + 1) console.log(indexOf(["John", "David", "Bob"], "Adam")) console.log(indexOf(["Mike", "Adam", "Carol", "Sam"], "Sam"))
To run the above program, you need to use the below command −
node fileName.js.
Here, my file name is demo321.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo321.js -1 3
- Related Articles
- How to return a value from a JavaScript function?
- Get the correct century from 2-digit year date value - JavaScript?
- JavaScript Quicksort recursive
- Return maximum value from records in MySQL
- Recursive multiplication in array - JavaScript
- Recursive Staircase problem in JavaScript
- JavaScript recursive loop to sum all integers from nested array?
- Is it required to have a return a value from a JavaScript function?
- Getting the return value of Javascript code in Selenium.
- How to fix Array indexOf() in JavaScript for Internet Explorer browsers?
- Ints indexOf() function in Java
- Return Top two elements from array JavaScript
- JavaScript code for recursive Fibonacci series
- Recursive product of summed digits JavaScript
- Recursive string parsing into object - JavaScript

Advertisements