Creating an Array
To create an array we use something called bracket syntax. It's more formally known as an array literal or array initializer and looks like this:
var arr = [element0, element1, ..., elementN];
elementN
is the list of values for the array's elements.
We'll use an example of an employee list to cover some basic things regarding arrays.
var emp = [];
emp[0] = 'Joe King';
emp[1] = 'Amanda Huggenkiss';
emp[2] = 'Justin Thyme';
console.log(emp);
Your console window should look like this:
[ 'Joe King', 'Amanda Huggenkiss', 'Justin Thyme' ]
Let's discuss the parts of our array:
array name -
emp
elements -
emp[0]
,emp[1]
, andemp[2]
element values -
'Joe King'
,'Amanda Huggenkiss'
, and'Justin Thyme'
Fun fact: We could have populated our array at the same time we created it!
var emp = ['Joe King', 'Amanda Huggenkiss', 'Justin Thyme'];
console.log(emp);
Last updated