Arrays

Arrays

An array is a data structure in JavaScript that allows you to store and organize multiple values(for elements) sequentially under a single variable name. These values can be any data type, such as numbers, strings, objects or even other arrays. Arrays provide a convenient way to work with collections of data in JavaScript.

Characteristics of Arrays

  1. Ordered Collection: Arrays maintain the order of elements in which they are inserted. Each element in an array has an index, starting from 0 for the first element

  2. Indexed Access: You can access elements in an array using their index. This allows for efficient retrieval and manipulation of individual elements.

  3. Dynamic Size: Unlike some other programming languages, JavaScript arrays are dynamic, meaning they can grow or shrink in size dynamically as elements are added or removed.

  4. Mixed Data Types: Arrays in JavaScript can contain elements of different data types within the same array.

Creating Arrays

You can create arrays in JavaScript using literals (‘[]’) or the array constructor

Accessing Arrays

You can access individual elements of an array using their index

Modifying Array Elements

You can modify array elements by assigning new values to specific indexes

Array Methods

  1. Mutator methods:

These methods modify the original array.

  • .push(): Adds one or more elements to the end of an array

  • .pop(): Removes the last element from the array and returns it

  • .shift(): Removes the first element from the array and returns it shifting all subsequent elements to a lower index.

  • .unshift(): Adds one or more elements to the beginning of an array shifting existing elements to higher indices.

  • .splice(): Changes the content of an array by removing or replacing existing elements and/or adding new elements in place.

  1. Accessor Methods:

These methods return information about the array without modifying it.

  • .concat(): Returns a new array that concatenates the original array with other arrays or values.

  • .slice(): Returns a shallow copy of a portion of an array into a new array.

  • .indexOf(): Returns the first index in which a given element can be found or -1 if it is not present.

  • lastIndexOf(): Returns the last index at which a given element is found in an array or -1 if it is not found.

  • includes(): Determines whether an array includes a certain element returning either true or false as appropriate.

  1. Iteration methods

These methods iterate over the element of the array and perform a callback function on each element.

  • forEach(callbackFn(element, index, array)): executes a provided function once for each element of the array

  • map(callbackFn(element, index, array)): Creates a new array populated with the results of calling a provided function on every element of the calling array.

  • filter(callbackFn(element, index, array)): Creates a new array with all elements that pass the test implemented by the provided function.

  • reduce(callbackFn(accumulator, current value, current index, array), initial value): Executes a reducer function on each element of the array resulting in a single output value.

  • reduceRight(callbackFn(accumulator,current value,current index,array),initial value):Similar to reduce() but processes the array from right to left

  1. Search methods

These methods search for specific elements or conditions within the array.

  • find(callbackFn(element, index, array)): Returns the value of the first element in the array that satisfies the provided testing function.

  • findIndex(callbackFn(element, index, array)): Returns the index of the first element in the array that satisfies the provided testing function.

  1. Others:
  • join(separator): joins all elements of an array into a string separated by the specified separator string.

  • toString(): Returns a string representing the array and its elements.

  • reverse(): Reverses the order of the elements in the array in place.

These methods provide powerful functionality for manipulating, accessing, and transforming arrays in JavaScript, making them a key tool for working with the collection of data