Array in JavaScript

What is an Array in JavaScript?

A pair of square brackets [] represents an array in JavaScript. All the elements in the array are comma(,) separated.

In JavaScript, arrays can be a collection of elements of any type. This means that you can create an array with elements of type String, Boolean, Number, Objects, and even other Arrays.

 Array.length

Returns the number of elements in an array

 Array.reverse()

Returns the array in reverse order

 Array.sort()

Sorts the elements of an array in specific order

 Array.fill()

Returns array by filling elements with given value

 Array.join()

Concatenates the array elements to a string

 Array.toString()

Returns the string representation of an array

 Array.pop()

Removes and returns the last array element

 Array.shift()

Removes and returns the first array element

 Array.push()

Adds elements to end of array & returns its length

 Array.unshift()

Adds elements to start of array and returns length

 Array.concat()

Returns array by merging given value and/or arrays

 Array.splice()

Returns an array by changing its elements in place

 Array.lastIndexOf()

Returns the last index of occurrence of an element

 Array.indexOf()

Returns the first index of occurrence of element

 Array.slice()

Returns a shallow copy of a portion of an array

 Array.findIndex()

Returns index of element that satisfies condition

 Array.find()

Returns first element that satisfies a condition

 Array.includes()

Checks if a value exists in an array

 Array.reduce()

Reduces array to single value from left to right

 Array.isArray()

Checks if the given value is a Array

 Array.filter()

method creates a new array filled with elements that pass a test provided by a function. does not change the original array.

 Array.map()

creates a new array by mapping elements using given function. does not change the original array.

 Array.forEach()

Executes the given function on array elements

 Array.values()

Returns iterator containing values of array items

 Array.from()

Creates a new Array from array like object

 Array.constructor

Returns the constructor function for the array

 Array.copyWithin()

Copies and overwrites elements within the array

 Array.toLocaleString()

Returns string representing the elements of array

  1. JavaScript Array Methods

    1. map( )

    This method creates a new array with the results of calling a provided function on every element in this array.

    2. filter( )

    This method creates a new array with only elements that pass the condition inside the provided function.

    3. sort( )

    This method is used to arrange/sort array’s elements either in ascending or descending order.

    4. forEach( )

    This method helps to loop over array by executing a provided callback function for each element in an array.

    5. concat( )

    This method is used to merge two or more arrays and returns a new array, without changing the existing arrays.

    6. every( )

    This method checks every element in the array that passes the condition, returning true or false as appropriate.

    7. some( )

    This method checks if at least one element in the array that passes the condition, returning true or false as appropriate.

    8. includes( )

    This method checks if an array includes the element that passes the condition, returning true or false as appropriate.

    9. join( )

    This method returns a new string by concatenating all of the array’s elements separated by the specified separator.

    10. reduce( )

    This method applies a function against an accumulator and each element in the array to reduce it to a single value.

    11. find( )

    This method returns the value of the first element in an array that pass the test in a testing function.

    12. findIndex( )

    This method returns the index of the first element in an array that pass the test in a testing function.

    13. indexOf( )

    This method returns the index of the first occurrence of the specified element in the array, or -1 if it is not found.

    14. fill( )

    This method fills the elements in an array with a static value and returns the modified array.

    15. slice( )

    This method returns a new array with specified start to end elements.

    16. reverse( )

    This method reverses an array in place. Element at last index will be first and element at 0 index will be last.

    17. push( )

    This method adds one or more elements to the end of array and returns the new length of the array.

    18. pop( )

    This method removes the last element from the end of array and returns that element.

    19. shift( )

    This method removes the first element from an array and returns that element.

    20. unshift( )

    This method adds one or more elements to the beginning of an array and returns the new length of the array.