forked from Pankaj-Str/JavaScript-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaScriptArray.js
More file actions
61 lines (36 loc) · 1.1 KB
/
JavaScriptArray.js
File metadata and controls
61 lines (36 loc) · 1.1 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
53
54
55
56
57
58
const fruits = ["apple", "banana", "cherry"];
// Array Constructor
const colors = new Array("red", "green", "blue");
// Array Elements
const mixedArray = [1, "apple", { name: "John" }, [2, 3, 4]];
const firstFruit = fruits[0]; // "apple"
const secondFruit = fruits[1]; // "banana"
console.log(firstFruit);
console.log(secondFruit);
fruits[2] = "grape";
console.log(fruits);
fruits.push("orange"); // adds element to the end of array
console.log(fruits);
fruits.pop(); // removes last element from array
console.log(fruits);
fruits.shift(); //removes first element from array
console.log(fruits)
fruits.unshift("kiwi"); //adds an element to the beginning of the array
console.log(fruits);
// Accessing elements with index
const myArray = [5,6,7,8,9];
let x = myArray[1]; //
// Using for loop
for (let i=0; i<myArray.length; i++) {
console.log(`The value at ${i} is ${myArray[i]}`);
}
// Using while loop
let j = 0;
while (j < myArray.length) {
console.log(`The value at ${j} is ${myArray[j]}`);
j++;
}
// Using for...of loop
for (const val of myArray){
console.log(`Value: ${val}`);
}