An array is a variable which can store many values inside it. A variable can only store one value. Those values can be accessed at any time using the array’s index. The values stored within the array are called the members.

Creating an array
To create an array, declare a variable to hold values of the array, and declare the new Array object.
[js]
var navArray = new Array();
[/js]
Alternately, you can also use a shorthand version, which will achieve the same outcome, where you wont have to declare the new array object.
[js]
var navArray = [];
[/js]
Similar to variables, you can setup a new array and assign the values in one line by using shorthand. The variables can be a mix of numbers and strings.
[js]
var navArray = [“home”, “catalog”, “about-us”];
[/js]
Accessing values in an array
To access the values that are now stored in the array, you use the array’s index. The index starts at 0, so if you wanted to access the “catalog” string, you would write the following:
[js]
var navArray[1];
[/js]
Add values to an array
You can add values to an array at any point since there are no restrictions . You do so by using the push method.
[js]
navArray.push(“careers”);
[/js]
Remove values from an array
You can remove values from an array by using two different methods. If you would like to remove the last item from an array, you would use the pop method.
[js]
navArray.pop();
[/js]
If you would like to add the value as the first item in the array, you would use the shift method.
[js]
navArray.shift();
[/js]
Length of an array
A useful tool that you can use when working with arrays is the length property, which will show you how many elements are contained within your array.
[js]
var navArray = [“apple”, “pear”, “orange”];
var navLength = navArray.length;
alert(navLength);
[/js]
Arrays always start at an index of 0 and the navLength variable in the proceeding example would return a value of 2 in the alert. In the following example, I am showing the index values of the navArray, so you can see how indexes are laid out.
[js]
navArray[0] = “apple”;
navArray[1] = “pear”;
navArray[2] = “orange”;
[/js]

Categories: BasicsJavaScript/Ajax

1 thought on “JavaScript Basics Part 5: Arrays”

Pikks · November 19, 2010 at 10:03 pm

Array.length is not a function, its a property. The above code will return an error.
Read more in https://developer.mozilla.org/en/JavaScript/Refer

Leave a Reply

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

Related Posts

Basics

JavaScript Basics Part 7: Loops

Loops are used to execute the same statement of code a set number of times, which can be hardcoded or based on variable. Loops are very common within programming, as they solve the problem of Read more…

Basics

JavaScript Basics Part 6: Conditionals

Logic is a part of our everyday lives – it’s a very familiar concept, therefore one of the easiest to understand. We are faced with questions all the time, with the “If” question being the Read more…

Basics

JavaScript Basics Part 4: Variables

Variables offer a way to store values of strings, numbers, and boolean data types, as well as being able to reuse at any point within your script. Declaring or creating variables is a common practice Read more…