# JavaScript Arrays 101

In JavaScript, an array is simply a collection of data stored in a single variable. It is hard to impossible to store large amount of data in different variables. And this where array comes in picture. Arrays help us store and manage multiple pieces of data in an organized way.

### Why Do We Need Arrays?

It is hard and inefficient to store large amounts of related data in different variables.

For example:

```plaintext
let fruit1 = "Apple";
let fruit2 = "Banana";
let fruit3 = "Mango";
```

This approach becomes difficult to manage when the number of items increases.

Instead, we can use an array:

```plaintext
let fruits = ["Apple", "Banana", "Mango"];
```

Now all related data is stored inside one variable, which makes the code cleaner and easier to work with.

### Creating an Array

Arrays are created using square brackets `[]`.

Example:

```plaintext
let fruits = ["Apple", "Banana", "Mango"];
```

In this array:

*   `"Apple"` is the first element
    
*   `"Banana"` is the second element
    
*   `"Mango"` is the third element
    

Arrays can store different types of values as well.

Example:

```plaintext
let data = ["Sam", 25, true];
```

### Accessing Elements Using Index

Each value inside an array has a position called an index.

Important thing to remember:

**Array indexing starts from 0 in JavaScript.**

Example:

```plaintext
let fruits = ["Apple", "Banana", "Mango"];

console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);
```

Output:

```plaintext
Apple
Banana
Mango
```

### Updating Elements in an Array

We can update an element by assigning a new value to its index.

Example:

```plaintext
let fruits = ["Apple", "Banana", "Mango"];

fruits[1] = "Orange";

console.log(fruits);
```

Output:

```plaintext
["Apple", "Orange", "Mango"]
```

Here we changed `"Banana"` to `"Orange"`.

### Array Length Property

JavaScript arrays have a built-in property called length. It tells us how many elements are inside the array.

Example:

```plaintext
let fruits = ["Apple", "Banana", "Mango"];

console.log(fruits.length);
```

Output:

```plaintext
3
```

This means the array contains 3 elements.

### Looping Through an Array

Very often we need to access all elements of an array. Instead of writing multiple console statements, we can use a loop.

One common way is using a for loop.

Example:

```plaintext
let fruits = ["Apple", "Banana", "Mango"];

for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}
```

Output:

```plaintext
Apple
Banana
Mango
```

The loop starts from index `0` and continues until it reaches the end of the array.

### Real-World Example

Imagine storing marks of a student.

```plaintext
let marks = [75, 80, 92, 68];

console.log(marks[0]); 
console.log(marks[2]);
```

Output:

```plaintext
75
92
```

## Summary

Arrays are one of the most commonly used data structures in JavaScript. They allow us to store multiple values in a single variable and access them using indexes. By understanding how to create arrays, access elements, update values, check their length, and loop through them, we can handle collections of data much more efficiently in our programs.
