UX Design and Development course

Working with Arrays

Syntax to follow - var yourArrayName = [];

Anything within the [] will become the array

Strings go in quotes, other values are simply entered. Remember to comma delineate each value in the array.

var names = ["Fred", "Amy", "Bert", "Mary-Ann"];
var ages = [66, 28, 7, 3]

console.log(names[1] + ' is ' + ages[1]);

Long hand way to write out an array

You don't want to do this if you are creating a simple list, but this helps to illustrate how an array can be built and have options to add and remove properties.

This has a downside that you have locked in the number of objects you can add to the array, and that could be bad.

var longList = new Array(3);
longList[0] = "Star Trek";
longList[1] = "Godzilla";
longList[2] = "Office Space";


Empty array

A better way to do this is to create an empty array and then as you work through the problem you are trying to solve, add the objects to the array as needed.

var emptyList = [];

emptyList[0] = "Old records";
emptyList[1] = "Old shoes";
emptyList[2] = "Old people";


Play with code

JS Bin