Skip to content

Commit 61fed07

Browse files
committed
update array
1 parent e3bb861 commit 61fed07

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

Example/Array/Example.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Document</title>
7+
<script src="JavaScriptArray.js"></script>
78
<script>
89

910
data = ['A','B','C','D'];

Example/Array/JavaScriptArray.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const fruits = ["apple", "banana", "cherry"];
2+
3+
4+
// Array Constructor
5+
6+
const colors = new Array("red", "green", "blue");
7+
8+
// Array Elements
9+
const mixedArray = [1, "apple", { name: "John" }, [2, 3, 4]];
10+
11+
12+
13+
const firstFruit = fruits[0]; // "apple"
14+
const secondFruit = fruits[1]; // "banana"
15+
16+
console.log(firstFruit);
17+
console.log(secondFruit);
18+
19+
fruits[2] = "grape";
20+
21+
console.log(fruits);
22+
23+
fruits.push("orange"); // adds element to the end of array
24+
25+
console.log(fruits);
26+
27+
fruits.pop(); // removes last element from array
28+
29+
console.log(fruits);
30+
31+
fruits.shift(); //removes first element from array
32+
33+
console.log(fruits)
34+
35+
fruits.unshift("kiwi"); //adds an element to the beginning of the array
36+
37+
console.log(fruits);
38+
39+
// Accessing elements with index
40+
const myArray = [5,6,7,8,9];
41+
42+
let x = myArray[1]; //
43+
44+
// Using for loop
45+
for (let i=0; i<myArray.length; i++) {
46+
console.log(`The value at ${i} is ${myArray[i]}`);
47+
}
48+
49+
// Using while loop
50+
let j = 0;
51+
while (j < myArray.length) {
52+
console.log(`The value at ${j} is ${myArray[j]}`);
53+
j++;
54+
}
55+
// Using for...of loop
56+
for (const val of myArray){
57+
console.log(`Value: ${val}`);
58+
}
59+
60+
61+

0 commit comments

Comments
 (0)