Last updated: 2026-05-05

Arrays and string methods

Array and string methods are built-in tools that allow you to manipulate or inspect data. Because JavaScript treats strings and arrays as objects under the hood, they come "pre-packaged" with these powerful functions.

These methods allow for the manipulation of data without having to write your own functions from scratch to perform common tasks.


Array Methods

Array methods are categorized based on how they interact with the data, whether by modifying the array, searching for elements, or iterating through them.

1. Basic Methods (Modification)

  • push() / pop() — Add or remove elements at the end of an array.
  • unshift() / shift() — Add or remove elements at the beginning.
  • slice() — Extracts a portion of an array into a new array without modifying the original.
  • splice() — Adds or removes elements from specific positions (mutates the original).

2. Iteration (Looping)

  • forEach() — Loops through elements. Best for when you just want to act on each element without returning anything.
  • map() — Transforms elements and returns a new array of the same length.
  • filter() — Creates a new array with elements that pass a specific condition.

3. Searching

  • indexOf() / lastIndexOf() — Find the first or last position of an element.
  • includes() — Checks if an element exists (returns true or false).
  • find() / findIndex() — Find an element or its position based on a logic condition.

4. Advanced Operations

  • reduce() — Accumulates array values into a single result (like a sum).

  • sort() / reverse() — Reorders the elements in the array. Note that sort() by default converts elements to strings and sorts them lexicographically — meaning [10, 2, 1].sort() returns [1, 10, 2] instead of [1, 2, 10]. For numerical sorting, a comparator function must be passed in:

    [10, 2, 1].sort((a, b) => a - b); // Output: [1, 2, 10]
    
  • concat() — Combines two or more arrays.

  • join() — Combines all elements into a single string.

  • flat() / flatMap() — Flattens nested arrays into a single level.


String Methods

Since strings are immutable, these methods always return a new string rather than changing the original one.

1. Basic Operations & Extraction

  • length — (Property) Returns the number of characters in the string.
  • toUpperCase() / toLowerCase() — Changes the casing of the text.
  • slice() / substring() — Extracts parts of a string.
  • charAt() — Accesses a character at a specific index.

2. Searching & Inspection

  • includes() — Checks if a substring exists within the string.
  • startsWith() / endsWith() — Checks if the string begins or ends with specific characters.
  • indexOf() — Returns the position of the first occurrence of a value.

3. Manipulation

  • replace() / replaceAll() — Replaces a specified value with another value.
  • split() — Converts a string into an array based on a separator (e.g., a comma or space).
  • repeat() — Returns a new string with a specified number of copies of the original.

4. Trimming and Padding

  • trim() — Removes whitespace from both ends.
  • trimStart() / trimEnd() — Removes whitespace from only one side.
  • padStart() / padEnd() — Pads the string with another string until it reaches a certain length.