Skip to main content

Command Palette

Search for a command to run...

07 JavaScript Arrays

Published
4 min read
07 JavaScript Arrays

Hey! readers in this blog we are going to understand about the Arrays , very important data structure , we will undertand what is the need of it?, where it used ? and how we can implement this in javaScript.

So let's proceed with the example:

Suppose there is no concept of arrays, and you are given with the task to store 7 names then how you are going to do this task? obviously using the variables you will be doing something like this

since it is 7 names so it seems do-able right? but what if i told you to store 1000 names or 10,000 names then how it sounds? still do-able but time consuming and also sounds difficult to create 1000 variables and manage them, So to solve these kind of problems arrays were introduced and with the help of arrays this is how we can perfom our tasks

So this is how with the help of arrays we can store all the names in the single variable only and we can access the values using the indexes (0,1,2..)


Note : In Javascript it is not necessary to have the element inside the array of same datatypes , we can store the elements having different datatypes as well for example

here you can see , we have storted the string, number, boolean inside the same array (and this is only possible in JS language.)


So we have already seen the array representation and the need of array, now we can say that it is a { collection of values stored in order }

Now let's take one example and perform some operations on the array


const arr = ["shubh",1,2,"work",true,{taks:"gym"}];

console.log(typeof arr) // output : object

Output : object (since everthing in javaScript is an object.)


Now let see how can we access the values

As you can see on the above example we are able to access the values of the array using indexes, which starts from 0 and go till the arr.length - 1;

  • also we can access the last element of the array by using arr[arr.length - 1] or arr.at(-1)

Now let see how can we modify the values of the array

Here on the above example , as you can see i have modified the value of index 5 to empty object and then printed the array and you can see over there it gets modified as empty object.


Let's see some intresting case

on the above example as you can see , I have checked the length of array using arr.length and it comes out as 6, but the intresting thing is that what we will get if we try to access the index which is greater than 6 , so i have tried to access the 10th index and guess what i have got undefined as a output, which is pretty obvious, also we will get same output if we try to access the negative indexes.


Let's discuss some basic looping in the array

I will write the loop name along with the implemented example for the syntax in this sections

  1. For loop :

  2. While loop :

  3. forEach loop :

So these are the basic loops that are generally used to iterate over the array.


Try it yourself :

  1. Create an array of 5 favorite movies

  2. Print the first and last element

  3. Change one value and print updated array

  4. Loop through the array and print all elements

  5. Also figure out some inbuilt methods of array (like .toReversed, .concat etc)


Conclusion:

Arrays are one of the most fundamental and useful data structures in JavaScript. They let you group related values under a single variable, access items by numeric indices (starting at 0), and manage large or dynamic collections without creating many separate variables. In JavaScript, arrays can hold values of different types, which makes them flexible for many tasks.


Hope you enjoyed reading❤️