Introduction

In this post, we will look at some commonly used JavaScript String, Array, and Object methods and some insights on choosing the right method based on the task.

Clicking on a method name opens a new tab with its MDN documentation. You can refer it for more information and usage examples.

String methods

Following are some of the common methods from the String global object.

  • indexOf() - This method returns the index of the passed string within the string. Returns -1 if the value is not found.
  • includes() - This method returns true or false based on whether the passed string exists within the string.

Use indexOf() if you need the position of the passed string in the string. Use includes() if you are only concerned whether the passed string exists, it makes intent of the code more clear.

  • trim() - This method removes whitespace from both ends of a string.
  • split() - This method returns an array of strings by separating the string at each instance of a specified separator string.
  • substring() - This method returns the part of the string between the start and end indexes.
  • slice() - This method is quite similar to substring() with some differences.

Array methods

Following are some of the common methods from the Array global object.

  • forEach() - This method executes a provided function once for each array element.
  • 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.
  • includes() - This method returns true or false based on whether the passed value is contained in the array.

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.

indexOf() and includes() use strict equality (the same method used by the 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.

Therefore, when dealing with array of objects, use some(), find(), or findIndex() methods.

  • find() - This method returns the value of the first element in the array that satisfies the provided testing function. Otherwise, undefined is returned.
  • findIndex() - This method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1.
  • 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.
  • every() - This method tests whether all elements in the array pass the test implemented by the provided function. It returns true or false appropriately.

Following methods are used for filtering and transforming an array.

  • filter() - This method creates and returns a new array with all elements that pass the test implemented by the provided function.
  • map() - This method creates and returns a new array with the results of calling a provided function on every element in the calling array.
  • 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.
  • splice() - This method modifies the contents of the array by adding or removing new elements in-place.
  • join() - This method is the opposite of String.prototype.split(). It creates and returns a new string by concatenating all array elements, separating them by a specified separator string.

Object methods

Following are some of the common methods from the Object global object.

  • keys() - This method returns an array of a given object’s own enumerable property names, in the same order as we get with a normal loop.
  • values() - This method returns an array of a given object’s own enumerable property values, in the same order as that provided by a for…in loop.
  • entries() - This method returns an array of a given object’s own enumerable string-keyed property [key, value] pairs, in the same order as that provided by a for…in loop.
  • fromEntries() - This method is the opposite of Object.entries(). It transforms a list of key-value pairs into an object.

One of the best ways to learn more these methods is to click on their links, read the documentation, and play around with some examples.