Arrays in JavaScript



In this Post I am going to explain the basic about JavaScript array and how can we retrieve data from JavaScript array.

As we all know array can be used to store number of value in a variables.

suppose if we want to store information of a customer/user instead of creating separate variables for storing name, email age and many more we may use a array to store all these information in a single variable.

Now the question arises that how a array is made in JavaScript?

The answer is pretty simple concept of creating array is same as other languages such as php the difference is all about syntax. let’s take a look on it

Creating an Array

For creating and storing data in array first we need to declare a array name as we do for other variable and then put the list of values which are separated by comma between opening and closing brackets: [ ] as shown below

var user = [‘Alex’, ‘alex@gamil.com’, ’23’];

NOTE: Don’t forget to use brackets because these brackets tells JavaScript interpreter that we want to create variable as array

Here we have created a array with name user and we provided users name, email and age.

There is one more way of creating array in JavaScript by using a keyword  new as shown below

var user = new Array(‘Alex’, ‘alex@gamil.com’, ’23’);

Array can be used to store mixed values like numbers, strings, and Boolean values.

Retrieving the value from array is also pretty simple The values form array can be retrieve by there respective index values

suppose we want at alert user name then we can do like this

alert(user[0]);

similarly if we want to alert user emailId then we can do like this alert(user[1]); and for age alert(user[1]);

 

Stay tuned for the PHP Magento, WordPress, JavaScript and Magento2 latest tutorial and updates, Hope you enjoyed reading, if you need the professional Magento Development / PHP we can help you, just Click on the Link and send me your requirements.

Please Like the Post on Facebook or Google+.