JavaScript Arrays 101

What arrays are and why we need them
Imagine you’re going grocery store for buying fruits . If you only need to buy 3-4 bananas, you will just carry it in your hand . but what happens when you need 10 apples, a carton of milk, and a loaf of bread? its obvious that you will get a basket.
In programming, an Array is that basket.
Why do we need them?
Without arrays, storing a simple list of fruits would look like this:
let f1 = "apple";
let f2 = "banana";
let f3 = "kiwi";
what if there are hundreds of fruits . it will be difficult to make hundred of variables and store the fruit values in that variable . This will make code very messy right . Here comes the use of ARRAY.
How to Create an Array
In javaScript, we create an array using square brackets []. Each item inside is separated by a comma. For example :
let fruits = ["safarjan", "kela", "kiwi", "aam"];
Understanding the "Index"
Programming counts starting from 0. The first fruit isn't "fruit 1," it's "fruit 0."
Index 0: safarjan
Index 1: kela
Index 2: kiwi
and so on .......
Accessing & Updating Elements
To access a specific fruit , you use its index number inside square brackets and same for updating too. For example :
// Accessing
console.log(fruits[0]); // Output: safarjan
// Updating
fruits[1] = "jamun"; // kela is now gone, replaced by jamun
Array length property
How many items are in your basket? You don't have to count them manually. Arrays have a built-in property called .length that tells you exactly how many elements are inside.
The syntax is : arrayname.length . For example
console.log(fruits.length); //Output:4
Basic Looping
Basic Looping means meeting every item
The most common thing we do with arrays is "loop" through them{all items} . A for loop allows us to visit every item in the "array" one by one. For example :
for (let i = 0; i < fruits.length; i++) {
console.log("I want to eat a " + fruits[i]);
}
Here is the practice of what you have read untill now :
THANK YOU!!
