Array Methods - JavaScript

Array Methods - JavaScript

What is Array?

In programming, an array is a collection of elements that are stored in a contiguous block of memory. An array can be defined as a collection of variables that are of the same data type, and are referred to by a common name. The elements of an array can be accessed using an index, which is an integer value that specifies the position of the element within the array.

const numbers = [1, 2, 3, 4, 5]; // Here is an example of creating an array

Array Methods :

push()

Adds one or more elements to the end of an array and returns the new length of the array.

pop()

Removes the last element from an array and returns that element.

let fruits = ['apple', 'banana', 'orange', 'mango'];

// Add a new element to the end of the array
fruits.push('kiwi'); // ['apple', 'banana', 'orange', 'mango', 'kiwi']

// Remove the last element from the array
let last = fruits.pop(); // 'kiwi'

shift()

Removes the first element from an array and returns that element.

unshift()

Adds one or more elements to the beginning of an array and returns the new length of the array.

let fruits = ['apple', 'banana', 'orange', 'mango'];

// Remove the First element from the array
fruits.shift('apple'); // ['banana', 'orange', 'mango'];

// Add a new element to the beginning of the array
fruits.unshift('strawberry'); // ['strawberry', 'apple', 'banana', 'orange', 'mango']

concat()

This method returns a new array that is the concatenation of two or more arrays.

let fruits = ['apple', 'banana', 'orange', 'mango'];

// Combine two arrays
let vegetables = ['tomato', 'cucumber', 'lettuce'];
let produce = fruits.concat(vegetables); // [ 'apple', 'banana', 'orange', 'mango', 'tomato', 'cucumber', 'lettuce']

slice()

Returns a new array that includes a sub-section of the original array, specified by start and end indices. It can take two arguments: start and end.

The start argument is the index at which to begin the slice and the end argument is the index at which to end the slice

const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const slice = numbers.slice(2, 5);  // Output: [2, 3, 4]

splice()

The splice() method is used to add or remove elements from an array. It can take up to three arguments: start, deleteCount, and items.

The start argument is the index at which to start changing the array, the deleteCount argument is the number of elements to remove, and the items argument is an optional list of elements to be added to the array.

//splice() to remove elements from an array:

const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const removed = numbers.splice(2, 3);  // Output: [2, 3, 4]
// numbers is now [0, 1, 5, 6, 7, 8, 9]

//splice() to add elements to an array:

const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const removed = numbers.splice(2, 0, 'a', 'b', 'c');  // Output: []
// numbers is now [0, 1, 'a', 'b', 'c', 2, 3, 4, 5, 6, 7, 8, 9]

Sort()

sort() method is used to sort the elements of an array in place and returns the sorted array. By default, the sort() method sorts the elements alphabetically for strings and in ascending order for numbers.

const numbers = [3, 1, 6, 2, 4, 5];
const sorted = numbers.sort();  // Output: [1, 2, 3, 4, 5, 6]

reverse()

The reverse() method is used to reverse the order of the elements in an array in place and returns the reversed array.

const numbers = [1, 2, 3, 4, 5];
const reversed = numbers.reverse();  // Output: [5, 4, 3, 2, 1]

join()

Converts all elements of an array to strings and joins them together, separated by a specified separator.

const elements = ['a', 'b', 'c'];
const joined = elements.join(', ');  // Output: 'a, b, c'

indexOf()

Returns the first index at which a given element can be found in the array, or -1 if it is not present.

const elements = ['a', 'b', 'c', 'b'];
const index = elements.indexOf('b');  // Output: 1

lastIndexOf()

Returns the last index at which a given element can be found in the array, or -1 if it is not present.

const elements = ['a', 'b', 'c', 'b'];
const index = elements.lastIndexOf('b');  // Output: 3

map()

The map() method in JavaScript is used to create a new array with the results of calling a provided function on every element in the array. It is a higher-order function, which means it takes a function as an argument and returns a new function.

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

// Use the map method to double each element in the array
let doubled = numbers.map(function(num) {
  return num * 2;
});

console.log(doubled);  // [2, 4, 6, 8, 10]