Top JavaScript String and Array Methods You Need to Know
Must-Know JavaScript String and Array Methods for Beginners
Hey there! If you're coming from a C++/C background like me, JavaScript can feel quite different, especially after a gap in practice. This blog post is here to serve as a quick reference guide to some of the most important methods for arrays and strings in JavaScript. Let's dive in and make sure you have these essentials at your fingertips!
Understanding Strings in JavaScript
First things first, JavaScript strings are primitive and immutable. This means that any method that seems to modify a string actually returns a new string with the changes, leaving the original string unchanged. This is crucial to remember to avoid confusion.
Common String Methods
Method | Description | Example |
str.toLowerCase() | Converts the entire string to lowercase. | str1 = str2.toLowerCase() |
str.toUpperCase() | Converts the entire string to uppercase. | str1 = str2.toUpperCase() |
str.slice(start,end) | Returns a part of the string between the start and end indices (end not included , indices < 0 treated as reverse indexing starts from -1). | str1 = str2.split("") |
str.split(delimiter) | Splits the string into an array based on the specified delimiter. | str1 = str2.split(",") |
str.split().sort().join() | Sorts the characters of the string (after splitting into an array). | str1 = str2.split("").sort().join("") |
str.substring(start, end) | Returns a part of the string between the start and end indices (end not included , indices < 0 treated as 0) | str1 = str2.substring(start, end) |
str.substr(start, length) | Returns a part of the string from the start index for the specified length. | str1 = str2.substr(start, length) |
str.length | Returns the length of the string. | size = str1.length |
Working with Arrays in JavaScript
Now let's talk about arrays. In JavaScript, arrays can be either mutable (methods that change the original array) or immutable (methods that return a new array without changing the original).
Common Array Methods
Method | Description | Example |
arr.length | Returns the length of the array. | let size = arr.length; |
arr.sort() | Sorts the elements of the array. A comparator function is often needed for numerical sorting. | arr.sort((a, b) => a - b); |
arr.toSorted() | Returns a new array with the elements sorted, without mutating the original array. | let sortedArr = arr.toSorted((a, b) => a - b); |
arr.reverse() | Reverses the order of the elements in the array. | arr.reverse(); |
arr.toReversed() | Returns a new array with the elements in reverse order, without mutating the original array. | let reversedArr = arr.toReversed(); |
arr.push(element) | Adds one or more elements to the end of the array. | arr.push(4); |
arr.pop() | Removes the last element from the array. | arr.pop(); |
arr.shift() | Removes the first element from the array. | arr.shift(); |
arr.unshift(element) | Adds one or more elements to the beginning of the array. | arr.unshift(1); |
arr.slice(start, end) | Returns a shallow copy of a portion of an array into a new array object.(end not included , indices < 0 treated as reverse indexing starts from -1). | let slicedArr = arr.slice(start, end); |
arr.splice(start, deleteCount, ...items) | Changes the content of an array by removing, replacing, or adding elements. | arr.splice(start, deleteCount, item1, item2, ...); |
arr.map (function) | Creates a new array with the results of calling a provided function on every element. | let mappedArr = arr.map (x => x * 2); |
arr.filter(function) | Creates a new array with all elements that pass the test implemented by the provided function. | let filteredArr = arr.filter(x => x > 5); |
Iterating Over Arrays and Objects
for...in
Loop: Used to iterate over the keys (indices) of an array or the properties of an object.for (let key in arr) { console.log(key); // Logs the index/key }
for...of
Loop: Used to iterate over the values of an array.for (let value of arr) { console.log(value); // Logs the value }
This guide should help you quickly revise the key string and array methods in JavaScript whenever you need to get back into development.