-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path029-arrays-in-js.js
More file actions
90 lines (66 loc) · 2.26 KB
/
029-arrays-in-js.js
File metadata and controls
90 lines (66 loc) · 2.26 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// 1: WHAT IS AN ARRAY?
const language1 = "JavaScript";
const language2 = "Python";
const language3 = "Java";
const languages = ["JavaScript", "Python", "Java"];
// 2: DECLARING ARRAYS
// Method 1: Array Literal (Most Common)
// const fruits = ["Apple", "Banana", "Orange"];
// // Method 2: Array Constructor
// const numbers = new Array(10, 20, 30);
// const emptyArray = new Array(5);
// console.log(emptyArray); // [empty × 5]
// console.log(emptyArray.length); // 5
// 3: ARRAY ELEMENTS & INDEX
// const colors = ["Red", "Green", "Blue"];
// console.log(colors[0]); // "Red"
// console.log(colors[1]); // "Green"
// console.log(colors[2]); // "Blue"
// console.log(colors[5]); // undefined
// 4: ARRAY LENGTH
// const fruits = ["Apple", "Banana", "Orange"];
// console.log(fruits.length); // 3
// 5: ARRAYS CAN HOLD DIFFERENT TYPES
// const mixedArray = ["Hello", 42, true, null, undefined];
// console.log(mixedArray);
// 6: TRAVERSING ARRAYS
// Example: Basic For Loop
// const fruits = ["Apple", "Banana", "Orange", "Mango"];
// for (let i = 0; i < fruits.length; i++) {
// console.log(fruits[i]);
// }
// 7: TRAVERSING WITH WHILE LOOP
// const numbers = [10, 20, 30, 40, 50];
// let i = 0;
// while (i < numbers.length) {
// console.log(numbers[i]);
// i++;
// }
// 8: PRACTICAL EXAMPLE
// const prices = [29.99, 49.99, 19.99, 99.99];
// let total = 0;
// for (let i = 0; i < prices.length; i++) {
// total += prices[i];
// }
// console.log("Total:", total); // Total: 199.96
// const numbers = [45, 12, 78, 33, 90, 23];
// let largest = numbers[0];
// for (let i = 1; i < numbers.length; i++) {
// if (numbers[i] > largest) {
// largest = numbers[i];
// }
// }
// console.log("Largest number:", largest); // 90
// IMPORTANT NOTES
// 1. Empty Arrays
// const tasks = [];
// console.log(tasks.length); // 0
// // 2. Accessing Last Element
// const items = ["First", "Second", "Third"];
// const lastItem = items[items.length - 1];
// console.log(lastItem); // "Third"
// // 3. const vs let for Arrays
// const myArray = [1, 2, 3];
// myArray[0] = 100; // This is fine!
// console.log(myArray); // [100, 2, 3]
// myArray = [4, 5, 6]; // ERROR!