[Go to site: main page, start]

0% found this document useful (0 votes)
6 views25 pages

Class 5) Array in Java Script

An array in JavaScript is a special variable that can hold multiple values, accessed by index numbers and written with square brackets. Key array methods include length, toString(), at(), join(), pop(), push(), shift(), and unshift(), which allow for various operations like adding, removing, and converting elements. Additionally, sorting methods such as sort() and toSorted() enable alphabetic sorting of arrays, with toSorted() preserving the original array.

Uploaded by

rajkumarleka1708
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views25 pages

Class 5) Array in Java Script

An array in JavaScript is a special variable that can hold multiple values, accessed by index numbers and written with square brackets. Key array methods include length, toString(), at(), join(), pop(), push(), shift(), and unshift(), which allow for various operations like adding, removing, and converting elements. Additionally, sorting methods such as sort() and toSorted() enable alphabetic sorting of arrays, with toSorted() preserving the original array.

Uploaded by

rajkumarleka1708
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like