ARRAY IN JAVA SCRIPT
What is an Array ?
An array is a special variable, which can hold more than one value.
An array can hold many values under a single name, and you can access
the values by referring to an index number.
JavaScript arrays are written with square brackets.
Syntax
Example
Output
Example :
Output
Array Methods In java Script
Basic Array Methods Are :
Array length
Array toString()
Array at()
Array join()
Array pop()
Array push()
Array length :
The length property returns the length (size) of an array:
JavaScript Array toString()
The JavaScript method toString() converts an array to a string of
(comma separated) array values.
JavaScript Array at()
The at() method returns an indexed element from an array.
The at() method returns the same as [].
JavaScript Array join()
The join() method also joins all array elements into a string.
It behaves just like toString(), but in addition you can specify
the separator:
Example :
Output
Popping and Pushing
When you work with arrays, it is easy to remove elements and
add new elements.
This is what popping and pushing is:
Popping items out of an array, or pushing items into an array.
JavaScript Array pop()
The pop() method removes the last element from an array:
The pop() method returns the value that was "popped out"
Example
Output
JavaScript Array push()
The push() method adds a new element to an array (at the end):
The push() method returns the new array length:
Example
Output
JavaScript Array shift()
The shift() method removes the first array element and "shifts"
all other elements to a lower index.
The shift() method returns the value that was "shifted out":
Example
Output
JavaScript Array unshift()
The unshift() method adds a new element to an array (at the
beginning), and "unshifts" older elements:
Example
Output
Array Sort Methods
Alpabetic Sort
Array sort()
Array reverse()
Array toSorted()
Array toReversed()
Sorting Objects
Sorting an Array
The sort() method sorts an array alphabetically:
Sorting an Array
the toSorted() method as a safe way to sort an array without altering
the original array.
The difference between toSorted() and sort() is that the first method
creates a new array, keeping the original array unchanged, while the
last method alters the original array.
Example
Output