JS Array Methods 🎯

beginner
11 min

JS Array Methods 🎯

Welcome to our deep dive into JavaScript (JS) Array Methods! In this comprehensive lesson, we'll explore various methods that make working with arrays in JS efficient and enjoyable. By the end of this tutorial, you'll be well-equipped to use these techniques in your own projects. Let's get started! 🎉

Table of Contents 📝

  1. Introduction to JS Arrays
  2. Accessing and Modifying Array Elements
  3. Adding and Removing Elements
  4. Searching and Finding Array Elements
  5. Sorting Arrays
  6. Manipulating Arrays
  7. Quiz

Introduction to JS Arrays 📝

An array in JavaScript is a special variable, which stores multiple values in a single variable. Arrays are ordered collections of items, which means they maintain the order in which elements are added.

javascript
// Declaring an array let myArray = [1, "apple", true];

Accessing and Modifying Array Elements 📝

To access an element in an array, we use its index number. Remember, index numbers start at 0 for the first element.

javascript
// Accessing an element let myArray = [1, "apple", true]; console.log(myArray[0]); // Output: 1

To modify an array element, simply assign a new value to the index.

javascript
// Modifying an element let myArray = [1, "apple", true]; myArray[0] = 10; console.log(myArray); // Output: [10, "apple", true]

Adding and Removing Elements 📝

Adding Elements

To add an element to an array, we can use the push() method, which appends the element to the end of the array.

javascript
// Adding an element let myArray = [1, "apple", true]; myArray.push("banana"); console.log(myArray); // Output: [1, "apple", true, "banana"]

You can also use the unshift() method to add an element at the beginning of the array.

javascript
// Adding an element at the beginning let myArray = [1, "apple", true]; myArray.unshift("orange"); console.log(myArray); // Output: ["orange", 1, "apple", true]

Removing Elements

To remove the last element from an array, use the pop() method.

javascript
// Removing the last element let myArray = ["orange", 1, "apple", true]; myArray.pop(); console.log(myArray); // Output: ["orange", 1, "apple"]

To remove an element at a specific index, use the splice() method.

javascript
// Removing an element at a specific index let myArray = ["orange", 1, "apple", true]; myArray.splice(1, 1); // Remove one element at index 1 console.log(myArray); // Output: ["orange", "apple", true]

Searching and Finding Array Elements 📝

Finding an Element

To find if an array contains a specific element, use the indexOf() method.

javascript
// Finding an element let myArray = ["orange", "apple", "banana"]; let isFound = myArray.indexOf("apple"); console.log(isFound > -1); // Output: true (since "apple" is found)

If the element is not found, the method returns -1.


Sorting Arrays 📝

To sort an array, use the sort() method.

javascript
// Sorting an array let myArray = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]; myArray.sort(); console.log(myArray); // Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

By default, sorting in JavaScript is case-sensitive. To sort strings in a case-insensitive manner, we can create a custom sorting function.

javascript
// Case-insensitive sorting let myArray = ["Apple", "apple", "banana", "Orange"]; myArray.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())); console.log(myArray); // Output: ["Apple", "Orange", "apple", "banana"]

Manipulating Arrays 📝

Joining Arrays

To join two or more arrays, use the concat() method.

javascript
// Joining arrays let array1 = [1, 2, 3]; let array2 = ["apple", "banana"]; let combinedArray = array1.concat(array2); console.log(combinedArray); // Output: [1, 2, 3, "apple", "banana"]

Slicing Arrays

To extract a specific portion of an array, use the slice() method.

javascript
// Slicing an array let myArray = [1, 2, 3, 4, 5]; let slicedArray = myArray.slice(1, 3); // Extract elements from index 1 to 2 (not inclusive of 3) console.log(slicedArray); // Output: [2, 3]

Quiz 🎯

Quick Quiz
Question 1 of 1

Which method can be used to add an element at the beginning of an array?


That wraps up our comprehensive tutorial on JS Array Methods! By now, you should be well-versed in various techniques for working with arrays. As you practice these methods, you'll see how they can help you build more efficient and effective JavaScript projects. Keep coding, and happy learning! 🚀