This page is a collection of commonly used classes and methods in JavaScript.
The following are methods of the global window.Array
class.
Keep in mind the following:
✓ no in-place modifications are made.
★ a completely new resulting string/array is returned.
**✗** can modify the input in-place.
In general, every time you need to specify a start index and end index, the start index is **always inclusive** and the end index is **always exclusive**.
// ═════╣ **Frequent ╠═════ \\\\
✓★** **map**((currElem, *i*) ⇒ newElem) // → Applies some transformation on all elements of the array.
// The callback *returns* the transformed element.
**✓★ filter**((currElem, *i*) ⇒ boolean) // → Get the array of elements that pass a condition.
// The callback is a *predicate* function. It returns true
// if the **currElem** should be 'kept' in the resultant array.
**✓★ reduce**( // → Aggregates the array to a single value.
(currVal, currElem, *i*) ⇒ newVal, // The **reducer** function, where **currVal** is the aggregated val so far (called
*initVal* // the **accumulator**). The function returns the new value for the **accumulator**.
**) // `initVal` is the starting value for the **accumulator**.
**✓ forEach**((currElem, *i*) => void) // → ****A more readable and less error-prone alternative to standard for looping.
**✓★ slice**(startIndex, endIndex) // → Makes a slice of the array at interval [startIndex, endIndex).
**✗ splice**(startIndex, runLen, *...items*) // → ****The swiss army knife for **removing** elements, **replacing** elements and **adding** new elements.
** // Consider: arr = ['Jan', 'Mar', 'April']
** // Add: arr.splice(1, 0, 'Feb') → ['Jan', 'Feb', 'Mar', 'April']
** // Remove: arr.splice(1, 2, 'Feb') → ['Jan', 'Feb']
** // Replace: arr.splice(1, 2, 'Feb', 'Mar') → ['Jan', 'Feb', 'Mar']
**✗ sort**((a, b) ⇒ number) // → Sorts the array in-place. For the **comparison** function:
// Return a **negative** number if you want a **before** b. (Note to self: remember, if you want a **<** b, then return **<** 0)
// Return a **positive** number if you want a **after** b. (Note to self: remember, if you want a **>** b, then return **>** 0)
// Return **0** if you want **a == b**.
**✗ reverse**() // → Reverse the array in-place.
**✓ join**(separator) // → Joins all elements into a string.
****// ═════╣ **Seldom ╠═════ \\\\
✓ every**((x, i) ⇒ boolean) // → Returns true if **all elements** pass the given predicate callback.
// *"For all elements x, pred(x) is true."*
**✓ some**((x, i) ⇒ boolean) // → Returns true if **at least 1** element passes the given predicate callback.
// *"There exists an element x such that pred(x) is true."*
**✓ includes**(targetElem, startIndex) // → Returns true if `targetElem` exists in the array.
// For more advanced use cases, the **some** method is probably better.
**✓ findIndex**((x, *i*) => boolean) // → Returns the index of the first element that passes the given predicate.
// Returns -1 if none found.
****
**✓ indexOf**(targetElem, startIndex) // → Returns the index of the first element equal to **targetElem**, or -1 if not found.
// For more advanced use cases, the **findIndex** method is probably better.
****// ═════╣ **Stack/Queue Operations ╠═════ \\\\
✗ push(...elems)** // → Append.
**✗ pop()** // → Pop back.
**✗ unshift(...elems)** // → Prepend.
**✗ shift()** // → Pop front.
The following are built-in string methods.
<aside> ℹ️ Strings are immutable. All string methods will return a new copy and you can expect no in-place modifications.
</aside>
// ═════╣ **Frequent ╠═════ \\\\
split**(delimitChar) // → Breaks up string into tokens, splitting on the given delimiter character.
**slice**(startIndex, endIndex) **** // → Same as Array.**slice**.
**search**(needle) // → Search for a string within this string (basically uses a string matching algorithm). Returns the index of the first match.
**search**(regexNeedle) // Alternatively, you can search by passing a regex.
****// ═════╣ **Normalisation ╠═════ \\\\
toLowerCase**() // → Converts all characters to lowercase.
**toUpperCase**() // → Converts all characters to uppercase.
**trim**() // → Strips leading and trailing whitespaces.
*... and [many more](<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#instance_methods>).*
****// ═════╣ **Seldom ╠═════ \\\\
indexOf**(targetVal, start) // Same as Array.**indexOf**.
**lastIndexOf**(targetVal, start) // Same as Array.**lastIndexOf**.
**includes**(targetVal) // Same as Array.**includes**. For more advanced usage, use **search**.
**substr**(startIndex, runLen) // Very similar to **slice**. Just use that.
Regex literals are defined with / /
.
Useful flags for global matching (finding all matches, not just one), ignoring case-sensitivity and multi-line are added after the second forward slash, eg. / /gim
.
const text = "hello 1 23 world 456 ";
const reg = /\\d+/gi;
****// ═════╣ **Useful Methods ╠═════ \\\\**
text.**match**(reg) // → Returns an array of substrings that match the regex (assuming the **g** flag is on)
// If no global matching, then it returns the **first** matching substring, but with
// more information about capture groups, index, etc.
reg.**test**(text) // → Returns true/false if **text** 'passes' the **regex** (ie. a match exists).
text.**replace**(reg, replacementStr) // → Replaces all matching substrings with **replacementStr** if **g** is on,
// else replace the first occurrence.