Array in JavaScript
Array in JavaScript
The first line creates an array of size 0, whereas the second line
specifies the size of an array.
Example:
Here, the last element a[4] of array a is a dense array with two
values 1 and 3. These two elements can be accessed using a
second set of subscripts as:
Num1= a[4][0];
Num2=a[4][1];
An example of array with elements of different data type is
given below
Array Methods
The JavaScript join() method is used to combine all elements of
an array into a single string, separated by a specified
separator. If no separator is provided, it defaults to a comma.
We use document.write() to output the result directly to the
webpage.
<!DOCTYPE html>
<html>
<head>
<title>Array join() Example</title>
</head>
<body>
<script>
// Define an array
const fruits = ["Apple", "Banana", "Cherry", "Date"];
</body>
</html>
Explanation:
fruits.join(" - ") combines the elements of fruits with " - " as
the separator.
document.write() outputs the joined string directly to the
webpage.
Reverse()
The JavaScript reverse() method reverses the order of elements
in an array in place. You can then use document.write() to
display the reversed array on the webpage.
<!DOCTYPE html>
<html>
<head>
<title>Array reverse() Example</title>
</head>
<body>
<script>
// Define an array
const fruits = ["Apple", "Banana", "Cherry", "Date"];
</body>
</html>
Explanation:
fruits.reverse() reverses the elements of the fruits array.
fruits.join(", ") is used to format the array into a string with
commas separating each element.
document.write() outputs the reversed array as a string
directly on the webpage.
Sort()
The JavaScript sort() method sorts the elements of an
array in place and can also accept a custom comparison
function. This method is also an inbuilt method.
<!DOCTYPE html>
<html>
<head>
<title>Array sort() Example</title>
</head>
<body>
<script>
// Define an array
const fruits = ["Banana", "Apple", "Cherry", "Date"];
</body>
</html>
Explanation:
fruits.sort() sorts the elements of the fruits array in
alphabetical order.
fruits.join(", ") combines the array elements into a string,
separated by commas.
document.write() outputs the sorted array as a string
directly on the webpage.
Cocat()
The JavaScript concat() method is used to merge two or more
arrays into a new array. It does not modify the original arrays
but returns a new one. You can use document.write() to display
the concatenated result on the webpage.
<!DOCTYPE html>
<html>
<head>
<title>Array concat() Example</title>
</head>
<body>
<script>
// Define two arrays
const fruits = ["Apple", "Banana"];
const berries = ["Strawberry", "Blueberry"];
// Use the concat() method to merge the arrays
const combinedArray = fruits.concat(berries);
</body>
</html>
Explanation:
fruits.concat(berries) merges the elements of the fruits and
berries arrays into a new array combinedArray.
combinedArray.join(", ") converts the array into a single
string with commas separating each element.
document.write() displays the result directly on the
webpage.
Pop()
The JavaScript pop() method removes the last element from an
array and returns that element. It modifies the original array by
shortening it by one element. You can use document.write() to
display the removed element or the modified array.
<!DOCTYPE html>
<html>
<head>
<title>Array pop() Example</title>
</head>
<body>
<script>
// Define an array
const fruits = ["Apple", "Banana", "Cherry", "Date"];
Push()
The JavaScript push() method adds one or more elements to
the end of an array and returns the new length of the array.
You can use document.write() to display the modified array
after using push().
<!DOCTYPE html>
<html>
<head>
<title>Array push() Example</title>
</head>
<body>
<script>
// Define an array
const fruits = ["Apple", "Banana", "Cherry"];
</body>
</html>
Explanation:
fruits.push("Date") adds the new element "Date" to the end
of the fruits array.
fruits.join(", ") converts the array into a comma-separated
string.
document.write() displays the modified array on the
webpage.
Slice()
The JavaScript slice() method creates a shallow copy of a
portion of an array into a new array, without modifying the
original array. You specify the starting index, and optionally the
ending index, to define the portion you want to extract.
<!DOCTYPE html>
<html>
<head>
<title>Array slice() Example</title>
</head>
<body>
<script>
// Define an array
const fruits = ["Apple", "Banana", "Cherry", "Date",
"Elderberry"];
</body>
</html>
Explanation:
fruits.slice(1, 4) extracts elements from index 1 up to (but
not including) index 4, resulting in a new array with
["Banana", "Cherry", "Date"].
slicedArray.join(", ") converts the sliced portion into a
comma-separated string.
document.write() displays the sliced array on the webpage.
Splice()
The JavaScript splice() method changes the contents of an
array by removing or replacing existing elements and/or adding
new elements in place. It modifies the original array and
returns an array containing the removed elements.
<script>
// Define an array
const fruits = ["Apple", "Banana", "Cherry", "Date",
"Elderberry"];
</body>
</html>
Explanation:
fruits.splice(1, 2) removes 2 elements starting from index 1.
In this case, "Banana" and "Cherry" are removed.
The removed elements are stored in removedElements,
which is then joined into a string for display.
document.write() is used to output both the removed
elements and the modified array.
<!DOCTYPE html>
<html>
<head>
<title>Array splice() Add Example</title>
</head>
<body>
<script>
// Define an array
const fruits = ["Apple", "Banana", "Cherry", "Date"];
</body>
</html>
Explanation:
fruits.splice(2, 0, "Elderberry", "Fig") adds "Elderberry" and
"Fig" at index 2 without removing any elements (the second
argument is 0).
The modified array is then displayed using document.write().
The splice() method can be for both removing and adding
elements in an array!