You are currently viewing What is Javascript Array with Example | Advanced Guide

What is Javascript Array with Example | Advanced Guide

What is an Array in Javascript Example

This article is about what is an array in javascript for example. The array is an important topic in every programming language or scripting language.

What is an array?

An array is a collection of data elements or data values. Arrays are created to store multiple values in a single variable. Normally, a variable is going to hold a single value if you want to store multiple values or a list of values in a single variable, then you create the variable of type array.

Note:

  • Each element in the array is going to have an index hence arrays are also called indexed variables.
  • Array follows “0” based indexing: i.e. the first element in the array is going to have an index of 0.

How to declare and initialize a variable of type array

There are two different ways in which we can create arrays in Javascript. They are:

Constructor notation

Syntax:

var arrayName = new Array(comma separated list of values);

Ex:

var studNames = new Array("Ram", "Ravi", "Raj", "John");

In this way, first, we write the “var” keyword then we write the array name and “equal to”. Then we use the “new” keyword then we call the array constructor function. To the array constructor function, we pass comma separated list of values as in the above example. I want to store a list of student names inside a variable. So, this is how you create an array using the constructor notation.

Literal notation

Syntax:

var arrayName = [comma separated list of values];

Ex:

var studNames=["Ram", "Ravi", "Raj", "John", "Alen"];

In the literal notation method, To create an array first you should write the “var” keyword then write the array name and “equal to”. Then use pair of square brackets inside that, we write comma separate list of values. See the above example code, var keyword is used to store a list of student’s names. So, the difference between constructor and literal notation is Instead of calling the “new” keyword in an array, we simply use pair of square brackets.

READ ALSO  JavaScript Not Equal Operator with an Example | Full Guide

What happens inside the memory

var studNames=["Ram", "Ravi", "Raj", "John", "Alen"];

In the above code, there are 5 array values, these 5 values ​​are stored in memory at each index. For example, the first value we have given is “Ram” in [0] index, “Ravi” value in [1] index, “Raj” value in [2] index, “John” value in [3] index, and the “Alen” value in [4] index are stored in memory.

How to get value for an array element

arrayName[index]

We can get a value of an array element by its “index”. The syntax is very simple, you just write the array name in between pair of square brackets you write the index of the element. So, if you want to access these values you take the help of the index.

Ex:

var stud=["Ram", "Ravi", "Raj", "John", "Alen"];
document.write(stud[1])

Result:what is array in javascript example
How to set a new value to an array

arrayname[index]=newvalue;

We can set a new value to an array element using the assignment operator which means “equal to” symbol (=). In the below example, 3 array values ​​are created in the first line of code. It stores at index 0, 1, and 2. We need to use index [3] to add the new value. If you give your new value in the index which is already stored then the previous values ​​will be removed and the new value given by you will be assigned

Ex:

var stud=["Ram", "Ravi", "Raj"];
stud[3]="Alex"
document.write(stud[3])

Result:what is array in javascript example

Conclusion

I suggest you experiment more with the code yourself, you will get more ideas if you experiment.

Leave a Reply