๐Ÿงฎ JavaScript Array [] and its methods ๐Ÿงฎ

๐Ÿงฎ JavaScript Array [] and its methods ๐Ÿงฎ

ยท

7 min read

JavaScript is a powerful and versatile programming language that allows developers ๐Ÿ‘จโ€๐Ÿ’ป to build complex applications with ease. One of the most fundamental data structures in JavaScript is the Array. Arrays are a collection of values that can be of any data type, stored in sequential order, and accessed by their index.In this blog post, we will explore JavaScript arrays in-depth and cover some of the most commonly used methods.

Creating an Array ๐Ÿ—

In JavaScript, an array can be created by simply enclosing a list of values in square brackets. Here is an example of an array that contains some numbers:

let numbers = [1, 2, 3, 4, 5];

This creates an array called numbers that contains five elements, each of which is a number. The first element has an index of 0, the second has an index of 1, and so on.

Arrays can also be created using the Array() constructor. Here is an example of how to create an array using the constructor :

let fruits = new Array("apple", "banana", "mango", "orange");

This creates an array called fruits that contains four elements, each of which is a string.

Accessing Array Elements ๐Ÿ› 

Array elements can be accessed using their index. Here is an example of how to access the second element in the numbers array:

let secondNumber = numbers[1];

This will assign the value 2 to the variable secondNumber.

Array Methods ๐Ÿ•น

JavaScript arrays come with a variety of built-in methods that can be used to manipulate the elements in the array. Here are some of the most commonly used array methods:

push()

The push() method modifies the original array by adding one or more elements to the end of the array.

let colors = ["red", "green", "blue"];
colors.push("yellow", "orange");
// colors is now ["red", "green", "blue", "yellow", "orange"]

pop()

The pop() method modifies the original array by removing the last element from the array and returning it.

let colors = ["red", "green", "blue"];
let lastColor = colors.pop();
// colors is now ["red", "green"]
// lastColor is "blue"

shift()

The shift() method modifies the original array by removing the first element from the array and returning it.

let colors = ["red", "green", "blue"];
let firstColor = colors.shift();
// colors is now ["green", "blue"]
// firstColor is "red"

unshift()

The unshift() method modifies the original array by adding one or more elements to the beginning of the array.

let colors = ["red", "green", "blue"];
colors.unshift("purple", "pink");
// colors is now ["purple", "pink", "red", "green", "blue"]

slice()

The slice() method does not modify the original array, but instead returns a new array that contains a copy of a portion of the original array.

let colors = ["red", "green", "blue"];
let someColors = colors.slice(1, 2);
// colors is still ["red", "green", "blue"]
// someColors is ["green"]

splice()

The splice() method modifies the original array by adding, removing, or replacing elements in the array.

let colors = ["red", "green", "blue"];
colors.splice(1, 1, "yellow", "orange");
// colors is now ["red", "yellow", "orange", "blue"]

In this example, splice() removes one element starting at index 1 and adds two new elements, "yellow" and "orange".

concat()

The concat() method does not modify the original array, but instead returns a new array that is a combination of two or more arrays.

let colors1 = ["red", "green"];
let colors2 = ["blue", "yellow"];
let allColors = colors1.concat(colors2);
// colors1 is still ["red", "green"]
// colors2 is still ["blue", "yellow"]
// allColors is ["red", "green", "blue", "yellow"]

filter()

The filter() method does not modify the original array, but instead returns a new array that contains only the elements that pass a certain test.

let numbers = [1, 2, 3, 4, 5];
let evenNumbers = numbers.filter(function(number) {
  return number % 2 === 0;
});
// numbers is still [1, 2, 3, 4, 5]
// evenNumbers is [2, 4]

In this example, filter() returns a new array that contains only the even numbers from the original numbers array.

map()

The map() method does not modify the original array, but instead returns a new array that contains the results of applying a function to each element in the array.

let numbers = [1, 2, 3, 4, 5];
let squaredNumbers = numbers.map(function(number) {
return number * number;
});
// numbers is still [1, 2, 3, 4, 5]
// squaredNumbers is [1, 4, 9, 16, 25]

In this example, map() returns a new array that contains the squares of each element in the original numbers array.

forEach()

The forEach() method does not modify the original array, but instead iterates over each element in the array and performs a function on each element.

let numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(number) {
  console.log(number);
});
// numbers is still [1, 2, 3, 4, 5]
// console output:
// 1
// 2
// 3
// 4
// 5

In this example, forEach() iterates over each element in the numbers array and logs the element to the console.

reduce()

The reduce() method does not modify the original array, but instead returns a single value that is the result of applying a function to each element in the array.

let numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce(function(total, number) {
  return total + number;
}, 0);
// numbers is still [1, 2, 3, 4, 5]
// sum is 15

In this example, reduce() returns the sum of all the elements in the numbers array.

every()

The every() method does not modify the original array, but instead returns a boolean value indicating whether all elements in the array pass a certain test.

let numbers = [1, 2, 3, 4, 5];
let allNumbersGreaterThanZero = numbers.every(function(number) {
  return number > 0;
});
// numbers is still [1, 2, 3, 4, 5]
// allNumbersGreaterThanZero is true

In this example, every() returns true because all elements in the numbers array are greater > than 0.

some()

The some() method does not modify the original array, but instead returns a boolean value indicating whether at least one element in the array passes a certain test.

let numbers = [1, 2, 3, 4, 5];
let someNumbersGreaterThanFour = numbers.some(function(number) {
  return number > 4;
});
// numbers is still [1, 2, 3, 4, 5]
// someNumbersGreaterThanFour is true

In this example, some() returns true because at least one element in the numbers array is greater than 4.

reverse()

reverse() is a method that reverse the order of the element in an array. The first element becomes the last, and the last element becomes the first. It modifies the original array and returns the reversed array. Here is an example:

const arr = [1,2,3,4,5];
arr.reverse();    // [5,4,3,2,1]
console.log(arr); // [5,4,3,2,1]

As you can see, the reverse() method reversed the order of the elements in the arr array.

sort()

sort() is a method that sorts the elements of an array. By default, it sorts the elements in ascending order based on their Unicode values. It modifies the original array and returns the sorted array. Here is an example:

const arr = [5,3,1,4,2];
arr.sort();    // [1,2,3,4,5]
console.log(arr);    // [1,2,3,4,5]

As you can see, the sort() method sorted the elements in the arr array in ascending order.

You can also provide a compare function to the sort() method to define your own sorting logic. The compare function should return a negative value if the first element should come before the second, a positive value if the first element should come after the second, and zero if the two elements are equal. Here is an example:

const arr = [5,3,1,4,2];
arr.sort((a,b) => b-a);    // [5,4,3,2,1]
console.log(arr)    // [5,4,3,2,1]

As you can see, the compare function (a, b) => b - a sorted the elements in the arr array in descending order.

It's important to note that sort() sorts the elements of an array in place, meaning that it modifies the original array. If you need to sort an array without modifying the original, you should make a copy of the array first using the slice() method.

Conclusion ๐Ÿค“

In conclusion, arrays are a fundamental data structure in JavaScript that allows you to store and manipulate collections of data. Understanding the various array methods available in JavaScript can greatly enhance your ability to work with arrays and process data effectively.

It's important to note that some array methods, such as sort() , splice(), push(), pop(), shift(), unshift(), reverse() modify the original array, while others, such as filter(), map() and reduce(), do not. By understanding which methods modify the original array and which methods do not, you can avoid unexpected behavior and ensure that your data is properly managed.

ย