How to Use Arrays in JavaScript

Arrays in JavaScript

Arrays are used to store multiple data values of the same type in programming languages but in JS we can also save data in an array of different types because it’s not a typed language.



An array is a special variable which can store more than one value at a time and latter we access them using there index number. So let’s make an array

In the code above it’s a simple array with name arry and have three items name one two and three. Now let’s create the same array this other method.

Both of above example are same.

JavaScript variables can be objects. Arrays are special kinds of objects.

Because of this, you can have variables of different types in the same Array.

You can have objects in an Array. You can have functions in an Array. You can have arrays in an Array. For example:

Now in the above code, we have an array which holds three types of different variables that’s the string, character and float number.

Accessing Arrays Items:

To access the elements of array index number are use. Like consider the following array

Now to access these items index no is used. The total items are three, so the total index use in the array are two because index count starts from 0. According to this on index 0 Mehran is set on 2 Honda and three civic.

To access an item the code is following:

In the code above the value at 0 indexes of cars is stored in variable x.


Array Properties and Methods:

There are many built-in JS array methods and properties like the length of array etc.

To insert some new item in array push() function is used. Instead of Push(), there are also many other ways to add some new value in the array.



output arrays in javascript
output arrays in javascript

Looping an Array:

If we display values one by one, it will take a lot of time. So we use loops to complete tasks. Like other programming languages, in JavaScript, we can also use loops.


Leave a Reply

Your email address will not be published. Required fields are marked *