Arrays in JavaScript
Arrays in JavaScript
In JavaScript, an array is a data structure that stores a collection of elements, which can be of any
data type, such as numbers, strings, objects, or even other arrays. Arrays in JavaScript are
dynamic, meaning they can grow or shrink in size as needed.
Creating an Array
Syntax:
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Arrays</title>
</head>
<body>
<p id="para"></p>
<script>
document.getElementById("para").innerHTML = marks;
</script>
</body>
</html>
Output:
Accessing Array Elements:
<script>
document.getElementById("para").innerHTML = marks[2];
</script>
Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays.
Arrays Methods:
JavaScript provides several built-in methods that you can use to manipulate arrays efficiently.
Here are some of the most commonly used array methods along with brief explanations:
push():
Adds one or more elements to the end of an array and returns the new length of the array.
<body>
<h2>Push Method</h2>
<p id="push"></p>
<p id="new"></p>
<script>
document.getElementById("push").innerHTML = marks.push(67);
document.getElementById("new").innerHTML = marks
</script>
</body>
pop():
Removes the last element from an array and returns that element.
<title>Array Methods</title>
</head>
<body>
<h2>Array Methods</h2>
<h3>Pop Method</h3>
<p id="pop"></p>
<p id="output"></p>
<script>
document.getElementById("pop").innerHTML = name;
name.pop();
document.getElementById("output").innerHTML = name;
</script>
</body>
</html>
shift():
Removes the first element from an array and returns that element. This method also updates the
length property of the array.
<body>
<h2>Shift Method</h2>
<p id="shift"></p>
<p id="new"></p>
<script>
document.getElementById("shift").innerHTML = marks.shift();
document.getElementById("new").innerHTML = marks
</script>
</body>
unshift():
Adds one or more elements to the beginning of an array and returns the new length of the array.
<body>
<h2>Unshift</h2>
<p id="unshift"></p>
<p id="new"></p>
<script>
document.getElementById("unshift").innerHTML = marks;
marks.unshift(77);
document.getElementById("new").innerHTML = marks;
</script>
</body>
splice():
Changes the contents of an array by removing or replacing existing elements and/or adding new
elements in place.
<body>
<h2>Splice Method</h2>
<p id="id"></p>
<p id="new"></p>
<script>
document.getElementById("id").innerHTML = name;
name.splice(1,0,"Aina", "Meno");
document.getElementById("new").innerHTML = name
</script>
</body>
slice():
Returns a shallow copy of a portion of an array into a new array object selected from start to end
(end not included). The original array will not be modified.
<body>
<h2>Slice Method</h2>
<p id="slice"></p>
<script>
</body>
</html>
concat():
<body>
<h2>Concat Method</h2>
<p id="hi"></p>
<script>
document.getElementById("hi").innerHTML = info
</script>
</body>
JavaScript Array toString:
The JavaScript method toString() converts an array to a string of (comma separated) array
values.
<body>
<h2>toString</h2>
<p id="para"></p>
<script>
document.getElementById("para").innerHTML = marks.toString();
</script>
</body>
: <body>
<h2>toString</h2>
<p id="para"></p>
<script>
// document.getElementById("para").innerHTML = marks.toString();
document.getElementById("para").innerHTML= size
</script>
</body>
Array Search Methods:
Array search methods in JavaScript refer to techniques used to find elements within an array
based on certain criteria. Here are some commonly used array search methods in JavaScript:
1. Indexof Method:
This method returns the first index at which a given element can be found in the array, or -1 if it
is not present.
<body>
<h2>Indexof Method</h2>
<p id="search"></p>
<script>
</script>
</body>
2. LastIndexof Method:
Similar to indexOf(), but starts the search from the end of the array.
<body>
<h2>lastIndexof Method</h2>
<p id="search"></p>
<script>
</script>
</body>
</html>
3. Find Method:
Returns the first element in the array that satisfies the provided testing function. Otherwise, it
returns undefined.
<body>
<h1>Find Method</h1>
<p id="search"></p>
<script>
</script>
</html>
</body>
4. findIndex() method
The findIndex() method returns the index of the first array element that passes a test function.
This example finds the index of the first element that is larger than 18:
<body>
<p id="ab"></p>
<script>
</script>
<body/>
5. includes():
Determines whether an array includes a certain value among its entries, returning true or false as
appropriate.
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = fruits.includes("Mango");
</script>
</body>
</html>
Array Sort Methods:
Sorting is the process of rearranging a sequence of elements in a specific order according to a
certain criterion. The criterion can vary depending on the context and requirements, such as
alphabetical order, numerical order, or custom-defined order based on specific properties or
rules.
Types:
Alphabetic Sort:
Alphabetic sorting refers to arranging elements based on their alphabetical order, following the
sequence of letters in the alphabet. When sorting strings alphabetically, each character in the
string is compared based on its Unicode code point value. The sorting algorithm organizes the
elements in ascending or descending order according to this sequence.
Methods:
I. Sort() Method:
Sorts Alphabetically
<body>
<p id="a"></p>
<p id="b"></p>
<script>
document.getElementById("a").innerHTML = name;
name.sort();
document.getElementById("b").innerHTML = name
</script>
</body>
</html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
document.getElementById("demo1").innerHTML = fruits;
fruits.reverse();
document.getElementById("demo2").innerHTML = fruits;
</script>
</body>
</html>
III. TOsorted Method:
method as a safe way to sort an array without altering the original array.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Arrays</h1>
<h2>The toSorted() Method</h2>
<p id="demo"></p>
<script>
const months = ["Jan", "Feb", "Mar", "Apr"];
const sorted = months.toSorted();
document.getElementById("demo").innerHTML = sorted;
</script>
</body>
</html>