-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path62 find and findIndex.html
37 lines (33 loc) · 1.21 KB
/
62 find and findIndex.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
//find() returns the first element that satisfies the condition
//findIndex() returns index of that element which satisfies the condition
const data = [
{ id: 1, tile: "First" },
{ id: 2, tile: "Second" },
{ id: 3, tile: "Third" },
{ id: 4, tile: "Fourth" },
];
//find()
console.log("find():")
const item = data.find((el) => el.id === 2);
console.log(item); // o/p: { id: 2, tile: "Second" },
//findIndex()
console.log("findIndex():")
const itemIndex = data.findIndex((el) => el.id === 1);
console.log(itemIndex); // o/p: 0
//extra
//The == operator performs a loose equality comparison that performs type coercion if
//necessary to make the comparison possible.
//The === operator, on the other hand, performs a strict equality comparison that does not
//perform type coercion and requires the operands to have the same type (as well as the same value).
</script>
</body>
</html>