String and Array methods in common
The easiest set of methods to learn is the methods that work for both arrays and strings. There are 5 methods that have the same name and same effect for both strings and arrays. There are also two “properties” in common to both strings and arrays.
The string and array slice() method
Use slice to return a copy or a portion of a string or array without mutating the original.
str.slice() to return a copy of the string.
arr.slice() to return a copy of the array.
The string and array concat() method
The concat method concatenates, or merges, two or more strings or arrays together by adding the value in the parentheses to the end of the calling string/array. Check out MDN String concat and Array concat for more information.
str.concat(str2, str3, …) to join 2 or more strings and return a copy of the joined strings.
arr.concat(arr2, arr3, …) to join 2 or more arrays and return a copy of the joined arrays.
NOTE: Using the
+= assignment operators used with strings accomplishes the same effect as
concat(). It is strongly recommended to use the assignment operators instead of the
concat() method with strings.
The string and array includes() method
str.includes(value) to perform a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.
arr.includes(value) to determine whether an array includes a certain value among its entries, returns true or false.
The string and array indexOf() method
str.indexOf(value) to return the first index at which a given element can be found in the string, or -1 if it is not present.
arr.indexOf(value) to return the first index at which a given element can be found in the array, or -1 if it is not present.
The string and array lastIndexOf() method
str.lastIndexOf(value) to return the last index at which a given element can be found in the string, or -1 if it is not present.
str.lastIndexOf(value) to return the last index at which a given element can be found in the array, or -1 if it is not present.
Two properties in common: length and bracket notation
The following two techniques are not methods but are common to both strings and arrays. Both .length and bracket notation are very useful and you probably are already aware of them, but I wanted to include them all the same.
arr.length to return the length of the string and array respectively.
arr[index] to return the string character and array value at the specified index respectively.
String and Array methods with different names but with similarities
Here are some other methods that have different names but have the same or opposite results. Personally, I do not use the first two methods and have only occasionally used the substring method.
charAt and at methods
Using str.charAt and arr.at returns the value at the index similar to bracket notation. There are reasons why you would use the arr.at() method over bracket notation (see MDN array at method), but I fail to see the application for the string charAt method.
str.charAt(index) to return the character at the index.
arr.at(index) to return the array item at the index.
Substring and splice (and slice) methods
Splice, slice, and substring all extract a part of an array or string. Substring and slice are identical with the exception of edge cases.
str.substring() to return a part of a string.
arr.splice() to return part of an array.
Slice and substring do not mutate the original source, but arr.splice() does. Check out MDN substring and array splice pages for more information. You should use all 3 methods on strings and arrays where applicable to understand the differences.
Split vs. Join methods
The split method is fantastic when working with strings. I believe I have only used the array join method in conjunction with split.
str.split() to turn a string into an array of substrings.
arr.join() to turn an array into a string.
NOTE: These two are often used in tandem. A common problem you will see is to turn a page or blog post title into a URL slug with a dash (-) as a separator. freeCodeCamp has an intermediate algorithm called Spinal Tap for that scenario but with some tricky test cases.