Arrays In JavaScript

Arrays In JavaScript

Array :-

Array in any programming language, is used to store list of elements or collection of multiple items which can be accessed by a single variable. In this blog we will learn and discuss about the applications of array methods.

-QhpVPV30.avif

Methods to add or remove elements :-

  • push( ) --> add an items to the end of the array.

  • pop ( ) --> remove an item from the end of the array.

  • shift ( ) --> remove an item from beginning of the array.

  • unshift ( ) --> Used to add an item to the beginning of the array.

  • concat ( ) --> It joins two different arrays and returns a new array.

  • slice (start, end ) --> It removes the elements from given array from index start to end and returns a new array containing removed elements.

  • splice (index, deleteCount, ...items) --> It removes the elements starting from index till deleteCount and adds ...items.

Note :- In methods like slice and splice starting point is inclusive and end point is exclusive.

for example :-

image.png

Some more methods :-

  • fill (value, start, end) --> changes elements in the array to static value from the start index to end index and returns the modified array.

image.png

image.png

image.png

  • filter ( ) --> It creates the shallow copy of the portion of the given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

image.png

  • copyWithhin( ) --> It copies part of an array to other location of the same array.

image.png

  • every ( ) --> This method tests whether all elements in the array pass the test implemented by the provided function. It returns a Boolean value.

image.png

  • forEach( ) --> It executes a provided function once for each array element.

image.png

  • indexOf ( ) --> It returns the first index at which the given element can be found in the array. If the element is not present then it will return -1.

image.png

  • join ( ) --> This method concatenates all the elements of the given array and returns the modified array.

image.png

  • map ( ) --> This method creates the new Array and the elements of this new array is a result of the function applied to every element of the given array.

image.png

  • sort( ) --> sorts the elements of an array and returns the reference to the same array.

image.png

  • toString( ) --> returns a string representing the specified array and its elements.

image.png

We have discussed some important methods of array. There are more to discuss.

@iwritecode

Did you find this article valuable?

Support Adesh Nayak's blog by becoming a sponsor. Any amount is appreciated!