Array Methods in Javascript

In JavaScript, an array is a data structure that allows you to store multiple values in a single variable. It is a container that holds a fixed number of elements, which can be of any type such as numbers, strings, objects, or even other arrays. Arrays are widely used in JavaScript and provide various methods and properties for working with and manipulating the data they store.

To create an array in JavaScript, you can use the array literal syntax, which involves enclosing a comma-separated list of values within square brackets:

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

Alternatively, you can use the Array constructor:

  let myArray = new Array(1, 2, 3, 4, 5);

Both approaches result in the creation of an array with the values [1, 2, 3, 4, 5].

Arrays in JavaScript are zero-indexed, meaning the first element is accessed using the index 0, the second element with the index 1, and so on. You can access individual elements of an array using square brackets and the corresponding index:

let myArray = [1, 2, 3, 4, 5];
console.log(myArray[0]);  // Output: 1
console.log(myArray[2]);  // Output: 3

You can also modify the value of an element by assigning a new value to its corresponding index:

let myArray = [1, 2, 3, 4, 5];
myArray[2] = 10;
console.log(myArray);  // Output: [1, 2, 10, 4, 5]

JavaScript arrays have a length property that returns the number of elements in the array:

let myArray = [1, 2, 3, 4, 5];
console.log(myArray.length);  // Output: 5

Arrays also provide numerous methods for performing operations such as adding or removing elements, iterating over the array, searching for specific values, sorting, and more. Some commonly used array methods include push(), pop(), splice(), concat(), slice(), forEach(), map(), filter(), find(), and sort().

Some commonly used array methods in JavaScript:

  1. push()
  2. Adds one or more elements to the end of an array and returns the new length of the array.

let myArray = [1, 2, 3];
let newLength = myArray.push(4, 5);  // Adds 4 and 5 to the end of the array
console.log(newLength);  // Output: 5 (new length of the array)
console.log(myArray);    // Output: [1, 2, 3, 4, 5] (updated array)

  1. pop()
  2. Removes the last element from an array and returns that element.

   let myArray = [1, 2, 3];
   let lastElement = myArray.pop();
   console.log(lastElement);  // Output: 3
   console.log(myArray);      // Output: [1, 2]

  1. shift()
  2. Removes the first element from an array and returns that element. It also updates the other elements’ indices.

   let myArray = [1, 2, 3];
   let firstElement = myArray.shift();
   console.log(firstElement);  // Output: 1
   console.log(myArray);       // Output: [2, 3]

  1. unshift()
  2. Adds one or more elements to the beginning of an array and returns the new length of the array. It also updates the other elements’ indices.

   let myArray = [2, 3];
   myArray.unshift(1);
   console.log(myArray);  // Output: [1, 2, 3]

let myArray = [3, 4, 5];
let newLength = myArray.unshift(1, 2);  // Adds 1 and 2 to the beginning of the array
console.log(newLength);  // Output: 5 (new length of the array)
console.log(myArray);    // Output: [1, 2, 3, 4, 5] (updated array)

  1. concat()
  2. Combines two or more arrays and returns a new array without modifying the original arrays.

   let array1 = [1, 2];
   let array2 = [3, 4];
   let newArray = array1.concat(array2);
   console.log(newArray);  // Output: [1, 2, 3, 4]

  1. slice()
  2. Returns a shallow copy of a portion of an array into a new array. It takes two optional parameters: start (inclusive) and end (exclusive).

   let myArray = [1, 2, 3, 4, 5];
   let slicedArray = myArray.slice(1, 4);//start at index 1(included) and end at index 4 (excluded)
   console.log(slicedArray);  // Output: [2, 3, 4]

You need to be logged in to view the rest of the content. Please . Not a Member? Join Us
Scroll to Top