JavaScript Array Methods provide powerful built-in functions to perform operations on array. These methods simplifies the operations like – adding, removing, searching, sorting, filtering, and other operations.
Here is an example that demonstrates the basic array operations.
// Initial array of numbers
let arr = [10, 20, 30, 40, 50];
// 1. Adding numbers to the array
arr.push(60); // Add 60 at the end
arr.unshift(5); // Add 5 at the beginning
console.log("After Adding Elements:", arr);
// Output: After Adding Elements: [ 5, 10, 20, 30, 40, 50, 60 ]
// 2. Filtering numbers greater than 50
let nums = arr.filter(num => num <= 30);
console.log("Numbers Less Than or Equal to 50:", nums);
// Output: Numbers Less Than or Equal to 50: [ 5, 10, 20, 30 ]
// 3. Doubling the values
let doubledNums = arr.map(num => num * 2);
console.log("Doubled Values:", doubledNums);
// Output: Doubled Values: [ 10, 20, 40, 60, 80, 100, 120 ]
// 4. Sorting in Descending Order
let sortedNums = arr.sort((a, b) => b - a);
console.log("Sorted in Descending Order:", sortedNums);
// Output: Sorted in Descending Order: [ 60, 50, 40, 30, 20, 10, 5 ]
Below are the list of all JavaScript array methods with their brief description.
Method/Property | Description |
---|---|
JavaScript Array() constructor | Creates a new array instance (Array object). |
JavaScript Array.from() Method | Creates an array from an array-like or iterable object. |
JavaScript Array.fromAsync() Method | Creates an array from an async iterable or promises. |
JavaScript Array.isArray() Method | Checks if a value is an array. |
JavaScript Array.of() Method | Creates an array from arguments provided. |
JavaScript Array at() Method | Returns the element at a specific index, supporting negative indices. |
JavaScript Array concat() Method | Merges two or more arrays into a new array. |
JavaScript Array copyWithin() Method | Copies part of an array to another location within the same array. |
JavaScript Array entries() Method | Returns an iterator with key-value pairs for array indices and values. |
JavaScript Array every() Method | Tests if all elements pass a given condition. |
JavaScript Array fill() Method | Fills elements with a static value from a start to an end index. |
JavaScript Array filter() Method | Creates a new array with elements that pass a condition. |
JavaScript Array find() Method | Returns the first element that satisfies a condition. |
JavaScript Array findIndex() Method | Returns the index of the first element that satisfies a condition. |
JavaScript Array findLast() Method | Returns the last element that satisfies a condition. |
JavaScript Array findLastIndex() Method | Returns the index of the last element that satisfies a condition. |
JavaScript Array flat() Method | Flattens nested arrays into a single-level array. |
JavaScript Array flatMap() Method | Maps and flattens the array in one operation. |
JavaScript Array forEach() Method | Executes a function for each array element. |
JavaScript Array includes() Method | Checks if an array contains a specified value. |
JavaScript Array indexOf() Method | Returns the index of the first occurrence of a value. |
JavaScript Array join() Method | Joins all elements into a string using a specified separator. |
JavaScript Array keys() Method | Returns an iterator for the array indices. |
JavaScript Array lastIndexOf() Method | Returns the index of the last occurrence of a value. |
JavaScript Array map() Method | Creates a new array by applying a function to each element. |
JavaScript Array pop() Method | Removes and returns the last element of an array. |
JavaScript Array push() Method | Adds one or more elements to the end of an array. |
JavaScript Array reduce() Method | Reduces the array to a single value using a function. |
JavaScript Array reduceRight() Method | Reduces the array to a single value, iterating from right to left. |
JavaScript Array reverse() Method | Reverses the order of array elements. |
JavaScript Array shift() Method | Removes and returns the first element of an array. |
JavaScript Array slice() Method | Returns a shallow copy of part of an array. |
JavaScript Array some() Method | Tests if at least one element passes a given condition. |
JavaScript Array sort() Method | Sorts the array in place based on a comparison function. |
JavaScript Array splice() Method | Adds or removes elements from an array at a specified index. |
JavaScript Array toLocaleString() Method | Converts elements to strings using locale settings. |
JavaScript Array toReversed() Method | Returns a new array with reversed elements without modifying the original array. |
JavaScript Array toSorted() Method | Returns a new array with sorted elements without modifying the original array. |
JavaScript Array toSpliced() Method | Returns a new array after splicing, without modifying the original array. |
JavaScript Array toString() Method | Converts the array to a string of elements separated by commas. |
JavaScript Array unshift() Method | Adds one or more elements to the beginning of an array. |
JavaScript Array values() Method | Returns an iterator for the array values. |
JavaScript Array with() Method | Returns a new array with an updated value at a specified index. |
JavaScript Array length Property | Returns the number of elements in the array. |