TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript Array find() Method

JavaScript Array find() Method with example, javascript array methods, concat() method, every() method, filter() method, forEach() method, join() method, indexOf() method, lastIndexOf() method, map() method, push() method, slice() method, sort() method, unshift() method, toSource() method etc.

<< Back to JAVASCRIPT

JavaScript Array find() method

The JavaScript array find() method returns the first element of the given array that satisfies the provided function condition.

Syntax

The find() method is represented by the following syntax:

array.find(callback(value,index,arr),thisArg)  

Parameter

callback - It represents the function that executes each element.

value - The current element of an array.

index - It is optional. The index of current element.

arr - It is optional. The array on which find() operated.

thisArg - It is optional. The value to use as this while executing callback.

Return

The value of first element of the array that satisfies the function condition.

JavaScript Array find() method example

Let's see some examples of find() method.

Example 1

Let's see a simple example of find() method.

<script>
var arr=[5,22,19,25,34];
var result=arr.find(x=>x>20);
document.writeln(result)
</script>
Test it Now

Output:

22

Example 2

In this example, we will find a prime number using find() method.

<script>
function isPrime(value, index, arr) {
  var start = 2;
  while (start <= Math.sqrt(value)) {
    if (value % start++ < 1) {
      return false;
    }
  }
  return value > 1;
}
document.writeln([8, 4, 7, 22].find(isPrime));
</script>
Test it Now

Output:

7

Next TopicJavaScript Array




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf