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: _posts/2019-11-28-Commonly used JavaScript functions.markdown
+55Lines changed: 55 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,32 @@ Use `indexOf()` if you need the position of the passed string in the string. Use
25
25
-[substring()][String.prototype.substring()] - This method returns the part of the string between the start and end indexes.
26
26
-[slice()][String.prototype.slice()] - This method is quite similar to `substring()` with some [differences][StackOverflow - slice() vs substring()].
27
27
28
+
## Array methods
29
+
Following are some of the common methods from the [Array][Array] global object.
30
+
31
+
-[forEach()][Array.prototype.forEach()] - This method executes a provided function once for each array element.
32
+
-[indexOf()][Array.prototype.indexOf()] - This method returns the first index at which a given element can be found in the array, or -1 if it is not present.
33
+
-[includes()][Array.prototype.includes()] - This method returns _true_ or _false_ based on whether the passed value is contained in the array.
34
+
35
+
Just like with `String` methods, use `indexOf()` if you need the position of the value in the array. Use `includes()` if you are only concerned whether the value exists, it makes intent of the code more clear.
36
+
37
+
`indexOf()` and `includes()` use [strict equality][Strict equality] (the same method used by the === or triple-equals operator) for comparing the search element with the elements of the Array. So, when used on an array containing objects, object references are compared not the actual values.
38
+
39
+
Therefore, when dealing with array of objects, use `some()`, `find()`, or `findIndex()` methods.
40
+
41
+
-[find()][Array.prototype.find()] - This method returns the value of the first element in the array that satisfies the provided testing function. Otherwise, `undefined` is returned.
42
+
-[findIndex()][Array.prototype.findIndex()] - This method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1.
43
+
-[some()][Array.prototype.some()] - This method tests whether **at least one element** in the array passes the test implemented by the provided function. It returns _true_ or _false_ appropriately.
44
+
-[every()][Array.prototype.every()] - This method tests whether **all elements** in the array passes the test implemented by the provided function. It returns _true_ or _false_ appropriately.
45
+
46
+
Following methods are used for filtering and transforming an array.
47
+
48
+
-[filter()][Array.prototype.filter()] - This method creates and returns a new array with all elements that pass the test implemented by the provided function.
49
+
-[map()][Array.prototype.map()] - This method creates and returns a new array with the results of calling a provided function on every element in the calling array.
50
+
-[reduce()][Array.prototype.reduce()] - This method executes a provided reducer function on each element of the array, resulting in a single output value. This final output value is returned.
51
+
-[splice()][Array.prototype.splice()] - This method modifies the contents of the array by adding or removing new elements in-place.
52
+
-[join()][Array.prototype.join()] - This method is the opposite of [String.prototype.split()][String.prototype.split()]. It creates and returns a new string by concatenating all array elements, separating them by a specified separator string.
0 commit comments