Working with Javascript Array find() method with an example for Beginners
Javascript array find() method is a built-in method whose purpose is to return the item which matches to a given condition. It returns only one first item even if the condition is matched to other items. The first item encountered will be returned. This method takes a function and object as arguments. Below is the syntax of the find() method. It also does not modify an existing array.
Syntax
array.find(function(current_value, index, array){}, extra_arguments);
The first argument must be a function it can be named or an anonymous function and the second argument must object which is optional.
If you don’t pass a function as an argument than it throws Uncaught TypeError: is not a function
exception.
All the arguments in the function are optional.
First argument in find()
the method takes three arguments they are current index value, index of the array, and complete array all these
arguments are optional but it is excepted that you must pass current_value
argument.
If you wish to pass additional arguments to function then you can make use of the second argument extra_arguments
as objects.
To access extra_arguments
in function use, this object returns additional parameters passed to the function.
Table of Contents
- Program to display non-negative even number in array
- Program to find a number in an array
- Conclusion
Program to display non-negative even number in an array
Variable arr
positive and negative numbers. Some of them are even and some are negative even number. We need to find the first number which is non-negative and also even number.
This logic is written inside find_non_negative_even_no
the function which returns true if the number matches the given condition.
let arr = [1,-6,3,-8,4,12]; function find_non_negative_even_no(number, index, arr){ if(number>0 && number%2==0){ return true; } } let result = arr.find(find_non_negative_even_no); console.log("The First Non-Negative Even Number is : "+result); //Console Output The First Non-Negative Even Number is : 4

program to display non-negative even number in array using javascript array find method
If all the numbers are negative even then the result will be undefined
let arr = [1, -6, 3, -8, -4, -12]; function find_non_negative_even_no(number, index, arr){ if(number>0 && number%2==0){ return true; } } let result = arr.find(find_non_negative_even_no); console.log("The First Non-Negative Even Number is : "+result); //Console Output The First Non-Negative Even Number is : undefined

Javascript non-negative even numbers
Program to find a number in an array
This program displays a message if the number exists in an array and if does not exist then it displays a message that “The number is not found in array”.
<label for="">Input Number to find in array</label> <input type="number" id="input-number" value=""> <br> <input type="button" onclick="search_for_number()" value="Find Number"> <p id="message" ></p> <script> let message_element = document.getElementById('message'); function search_for_number(){ let arr = [12, 56, 45.26, 18, 24]; let result = arr.find(find_number, { given_number : Number(document.getElementById('input-number').value) }); let msg = ""; if(result!=undefined){ msg = "The number is found in array"; }else{ msg = "The number is not found in array"; } message_element.innerHTML = msg; } function find_number(number, index){ let given_number = this["given_number"]; return (number===given_number); } </script>

Find the number in javascript using the array.find() method
Conclusion
We have reached the conclusion of our post Working with Javascript Array find() method with an example for Beginners. Comment if you have any doubts and if you like then help us grow by sharing this post.
Related Posts
- How to create a Beep Sound in Javascript with Snippets and Examples
- Javascript Insert Before and After HTML Element or Node with Snippet




