Tuesday, December 17, 2013

JavaScript Tutorial 4 - Arrays

An array is a special variable, which can hold more than one value at a time.
If you have a list of items instead of creating multiple var statements you can just use an array to store all the data in one line of code. For example...

Var names = [Nick,Joe,Bryan];

Ok so now we have this piece of data, but how do we access it you ask? Well each piece of data has a corresponding index number where it can be accessed from. Nick = 0 , Joe = 1 and 
Bryan = 2 if you had more data you would just keep counting up by 1. So now to see how this looks in JavaScript...

var Myname = names[0]
Myname = Nick

var Myname = names[1]
Myname = Joe

var Myname = names[2]
Myname = Bryan

Now you can also change the data in the array by using a statement like this...

names[0] = "Luke"

That line of code just changed the result of the previous code that pulled the first peice of data in the array so now if we run the code...

var Myname = names[0]
Myname = Luke

We see that Nick changed to Luke due are previous code. This is a brief overview of Arrays in JavaScript.

No comments:

Post a Comment