Skip to main content

Command Palette

Search for a command to run...

JavaScript Arrays

Updated
4 min read
JavaScript Arrays

In the previous blog, we explored control flow statements in JavaScript such as if, if-else, and switch. These statements help us control the direction of program execution based on different conditions.

JS CONTROL FLOW STATEMENTS

By using them, we can make our code more dynamic, decision-based, and efficient. Now, building on that foundation, let’s move forward to understand the next important concept in JavaScript.

Suppose you want to store the name of 10 students so how do you store them in 10 variables?okay that's fine but if you have to store the name of 100 student you use 100 variables? ohh now its become tough to store 100 students name because it difficult to create 100 variables and manage them so here arrays comes in the picture. Arrays are ordered collection of items . In array you can store multiple items under one variable name.

Let create a fruit array in which i will collect 4-5 fruits name.

so this is a list of fruits . we can access any values using indexed. The number that are starting from zero are indexes using that we can access any items.

let fruits=["apple","mango","bannana","litchi","orange"]

so above we have crearted a array of fruits we can write the items in the [] brackets by seperating each elements with comma( , ). Arrays are flexible so you can store different types of value in a array.

 let fruits=["apple","mango","bannana","litchi","orange",7]

this is also a valid array ya but don't make any sense to store a number seven in an array named fruits. So now let see how we can access elements using indexes.

let fruits=["apple","mango","bannana","litchi","orange"]

console.log(fruits[1])

//Output 
mango

We can access any element of array using square bracket. First write array name then square brackets and inside square bracket write the index of that element.

let fruits=["apple","mango","bannana","litchi","orange"]

console.log(fruits[0])
console.log(fruits[3])
//Output 
apple
litchi

but wait now i have a question what if i want to access the last element and you dont know about the size of array so how you can print the last element?

Don't worry we have a length property in array through which we can access the last elment.

let fruits=["apple","mango","bannana","litchi","orange"]
console.log(fruits.length)

//Output
5

Using length property we can get the length of the array but arrays are zero indexed so the last index should be length -1 .

let fruits=["apple","mango","bannana","litchi","orange"]
lastIndex=fruits.length -1;
console.log(fruits[lastIndex])

//Output
orange

so now we have understand how we can create an array and how we can access any element.

Now lets see how we can update any value.

let fruits=["apple","mango","bannana","litchi","orange"]
fruits[1]="watermelon"

console.log(fruits[1])

//Output
watermelon

Using the same square bracket notation we can update the value at a specific index.

Looping over arrays:

Suppose you have created an array in which you have stored the marks of student and every student is getting a grace of 5 marks in that subject and we have update this in the array so how you can do this?

By manually updating every index is a tough task because the size of array can be very large, so what we can do we can loop over an array we use loops when we want to repeat the same task , so we can loop on the indexes of the array and then using indexes we can modify any value.

let marks=[55,56,73,45,88,43,66,64,78]

for(let i=0;i<marks.length;i++){
  marks[i]=marks[i]+5;
}

We can also print all the values of an array using loop let print the updated value of marks array.

let marks=[55,56,73,45,88,43,66,64,78]

for(let i=0;i<marks.length;i++){
  marks[i]=marks[i]+5;
}
for(int i=0;i<marks.length;i++){
console.log(marks[i])
}

//Output

60, 61, 78, 50, 93, 48, 71, 69, 83

Conclusion:
In this blog, we learned what arrays are and why they are useful for storing multiple values under a single variable name. We explored how to create arrays, access elements using indexes, find the last element with the length property, update values, and loop through arrays to perform operations like adding marks. Arrays make our code more organized, flexible, and efficient when working with a collection of data.