0% completed
JavaScript arrays are flexible and powerful, capable of storing multiple values in an ordered list. They come with a variety of built-in properties and methods that allow developers to manipulate these lists in nearly any conceivable way—adding, removing, sorting, searching, slicing, and more. Understanding these methods and properties is essential for efficient data handling and manipulation in JavaScript.
JavaScript arrays offer several useful properties:
Sr.No | Property | Description |
---|---|---|
1 | length | Returns the number of elements in the array. |
Here's an overview of some commonly used array methods:
Sr.No | Method Name | Description |
---|---|---|
1 | at() | Retrieves an element at a given index, allowing for positive and negative integers. |
2 | concat() | Combines the array with other array(s) or value(s) and returns a unified array. |
3 | copyWithin() | Copies array elements within the array to and from specified positions. |
4 | entries() | Provides an iterator that contains key/value pairs for each index in the array. |
5 | every() | Checks if all elements in the array pass a test defined in a provided function. |
6 | fill() | Fills all the elements of an array from a start index to an end index with a static value. |
7 | filter() | Creates a new array with elements that pass the test provided by a function. |
8 | find() | Finds the first element in the array that satisfies the provided testing function. |
9 | findIndex() | Finds the index of the first element in the array that satisfies the provided testing function. |
10 | findLast() | Finds the last element in the array that satisfies the provided testing function. |
11 | findLastIndex() | Finds the index of the last element in the array that satisfies the provided testing function. |
12 | flat() | Flattens the array to the specified depth. |
13 | flatMap() | First flattens the array a single level deep, then maps each element using a mapping function. |
14 | forEach() | Executes a provided function once for each array element. |
15 | includes() | Determines whether an array includes a certain value, returning true or false. |
16 | indexOf() | Finds the first index of a given element in the array, or -1 if it is not present. |
17 | join() | Joins all elements of an array into a string and returns this string. |
18 | keys() | Returns a new array iterator that contains the keys for each index in the array. |
19 | lastIndexOf() | Returns the last index at which a given element can be found in the array, or -1 if it is not present. |
20 | map() | Creates a new array populated with the results of calling a provided function on every element in the array. |
21 | pop() | Removes the last element from an array and returns that element. |
22 | push() | Adds one or more elements to the end of an array and returns the new length of the array. |
23 | reduce() | Applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. |
24 | reduceRight() | Applies a function against an accumulator and each element in the array (from right to left) to reduce it to a single value. |
25 | reverse() | Reverses the order of the elements in an array in place. |
26 | shift() | Removes the first element from an array and returns that removed element. |
27 | slice() | Returns a shallow copy of a portion of an array into a new array object. |
28 | some() | Tests whether at least one element in the array passes the test implemented by the provided function. |
29 | toSource() | Generates a string representing the source code of the array. |
30 | sort() | Sorts the elements of an array in place and returns the array. |
31 | splice() | Changes the contents of an array by removing or replacing existing elements and/or adding new elements. |
32 | toLocaleString() | Converts the elements of the array to strings and concatenates them, separated by a locale-specific String. |
33 | toReversed() | Creates a new array with the elements of the calling array reversed. |
34 | toSpliced() | Creates a new array by removing, replacing, or adding elements at a specific index. |
35 | toString() | Converts an array and its elements to a string. |
36 | unshift() | Adds one or more elements to the beginning of an array and returns the new length of the array. |
37 | values() | Returns a new array iterator object that contains the values for each index in the array. |
38 | with() | Returns a new array with an element at a given index replaced with a new value. |
In this example, we'll determine the number of elements in an array.
let fruits = ["Apple", "Banana", "Cherry"];
creates an array named fruits
with three elements.console.log(fruits.length);
logs the number of elements in the fruits
array to the console. The length
property returns 3
, indicating there are three elements in the array.In this example, we'll add a new element to the end of an array.
fruits.push("Durian");
adds "Durian" to the end of the fruits
array.push()
method increases the array's length by one.fruits
array now shows four elements, including the newly added "Durian".In this example, we'll remove the last element from an array.
let removedFruit = fruits.pop();
removes the last element from the fruits
array and stores it in removedFruit
.pop()
method decreases the array's length by one and returns the removed element.removedFruit
shows "Cherry", the element that was removed.fruits
array now only contains two elements, as "Cherry" has been removed.These examples illustrate how to use the length
property to determine the size of an array, and the push()
and pop()
methods to add and remove elements, respectively. Understanding and effectively utilizing these array properties and methods is crucial for manipulating lists of data in JavaScript.
.....
.....
.....