-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.html
More file actions
54 lines (39 loc) · 1.33 KB
/
array.html
File metadata and controls
54 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Array Methods Example</title>
</head>
<body>
<h1>Array Methods Example</h1>
<h2>Original Array:</h2>
<p id="originalArray"></p>
<h2>Modified Array:</h2>
<p id="modifiedArray"></p>
<h2>Removed Item:</h2>
<p id="removedItem"></p>
<h2>Removed First Item:</h2>
<p id="removedFirstItem"></p>
<h2>Index of "banana":</h2>
<p id="bananaIndex"></p>
<h2>Array as a String:</h2>
<p id="arrayAsString"></p>
<script>
// Sample array containing both string and number elements
let data = [10, 'apple', 25, 'banana', 40, 'cherry'];
// Display the original array on the page
document.getElementById('originalArray').textContent = data.join(', ');
// Using push() to add an element at the end of the array
data.push('grape');
// Using pop() to remove the last element from the array
let removedItem = data.pop();
document.getElementById('removedItem').textContent = removedItem;
// Using shift() to remove the first element from the array
let firstItem = data.shift();
document.getElementById('removedFirstItem').textContent = firstItem;
// Using unshift() to add elements to the beginning of the array
data.unshift(5, 'orange');
</script>
</body>
</html>