

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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 Questions & Answers
- Get the correct century from 2-digit year date value - JavaScript?
- How to return a value from a JavaScript function?
- How to return value from an asynchronous callback function in JavaScript?
- JavaScript Quicksort recursive
- Return maximum value from records in MySQL
- Program to find dropped correct sensor value from the faulty list in Python
- Recursive Staircase problem in JavaScript
- JavaScript recursive loop to sum all integers from nested array?
- Placing integers at correct index in JavaScript
- Is it required to have a return a value from a JavaScript function?
- Recursive multiplication in array - JavaScript
- Ints indexOf() function in Java
- Getting the return value of Javascript code in Selenium.
- Get key from value in JavaScript
- How to fix Array indexOf() in JavaScript for Internet Explorer browsers?
Advertisements