How to Find First Occurrence of Array in Javascript?

25-Apr-2023

.

Admin

How to Find First Occurrence of Array in Javascript?

This tutorial will provide example of how to find first occurrence of array in javascript. I would like to share with you javascript array find | find first occurrence of array. I would like to share with you find the first occurrence in an array list in javascript. In this article, we will implement a how to find the first occurrence of an array in javascript . Follow bellow tutorial step of javascript find first occurrence in array.

To find the first occurrence of an array in JavaScript, you can use the `indexOf()` method.

Example 1:


<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>How to Find First Occurrence of Array in Javascript? - NiceSnippets.Com</title>

</head>

<body>

</body>

<script type="text/javascript">

const arr1 = [1, 2, 3];

const arr2 = [4, 5, 6];

const arr3 = [1, 2, 3];

console.log(arr1.indexOf(arr2)); // -1 (not found)

console.log(arr1.indexOf(arr3)); // 0 (found at index 0)

</script>

</html>

In this example, we have three arrays: `arr1`, `arr2`, and `arr3`. We use the `indexOf()` method to search for `arr2` in `arr1`, which returns `-1` because it is not found. We then search for `arr3` in `arr1`, which returns `0` because it is found at the beginning of the array.

#JavaScript