You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/05-array-methods/article.md
+42-13
Original file line number
Diff line number
Diff line change
@@ -234,12 +234,13 @@ Now let's cover methods that search in an array.
234
234
235
235
### indexOf/lastIndexOf and includes
236
236
237
-
The methods [arr.indexOf](mdn:js/Array/indexOf), [arr.lastIndexOf](mdn:js/Array/lastIndexOf)and [arr.includes](mdn:js/Array/includes) have the same syntax and do essentially the same as their string counterparts, but operate on items instead of characters:
237
+
The methods [arr.indexOf](mdn:js/Array/indexOf)and [arr.includes](mdn:js/Array/includes) have the similar syntax and do essentially the same as their string counterparts, but operate on items instead of characters:
238
238
239
239
-`arr.indexOf(item, from)` -- looks for `item` starting from index `from`, and returns the index where it was found, otherwise `-1`.
240
-
-`arr.lastIndexOf(item, from)` -- same, but looks for from right to left.
241
240
-`arr.includes(item, from)` -- looks for `item` starting from index `from`, returns `true` if found.
242
241
242
+
Usually these methods are used with only one argument: the `item` to search. By default, the search is from the beginning.
A minor, but noteworthy feature of `includes` is that it correctly handles `NaN`, unlike `indexOf`:
264
271
265
272
```js run
266
273
constarr= [NaN];
267
-
alert( arr.indexOf(NaN) ); // -1 (should be 0, but equality test === doesn't work for NaN)
274
+
alert( arr.indexOf(NaN) ); // -1 (wrong, should be 0)
268
275
alert( arr.includes(NaN) );// true (correct)
269
276
```
277
+
That's because `includes` was added to JavaScript much later and uses the more up to date comparison algorithm internally.
278
+
````
270
279
271
-
272
-
### find and findIndex
280
+
### find and findIndex/findLastIndex
273
281
274
282
Imagine we have an array of objects. How do we find an object with the specific condition?
275
283
@@ -309,7 +317,28 @@ In real life arrays of objects is a common thing, so the `find` method is very u
309
317
310
318
Note that in the example we provide to `find` the function `item => item.id == 1` with one argument. That's typical, other arguments of this function are rarely used.
311
319
312
-
The [arr.findIndex](mdn:js/Array/findIndex) method is essentially the same, but it returns the index where the element was found instead of the element itself and `-1` is returned when nothing is found.
320
+
The [arr.findIndex](mdn:js/Array/findIndex) method has the same syntax, but returns the index where the element was found instead of the element itself. The value of `-1` is returned if nothing is found.
321
+
322
+
The [arr.findLastIndex](mdn:js/Array/findLastIndex) method is like `findIndex`, but searches from right to left, similar to `lastIndexOf`.
0 commit comments