Individual Elements and Length
Individual Elements
If we wanted to know exactly who the second employee in our list was we could run this code:
var emp = ['Joe King', 'Amanda Huggenkiss', 'Justin Thyme'];
console.log(emp[1]);
Your console should have displayed Amanda Huggenkiss
. Remember we use emp[1]
because arrays are zero-indexed.
Length
There's also a way to find out the length of our array:
var emp = ['Joe King', 'Amanda Huggenkiss', 'Justin Thyme'];
console.log(emp['length']);
Our output is 3
. This is obviously because there are three elements in our array. You can also think of it as 3 is one more than the index of our final element. You could also write something like this:
var emp = ['Joe King', 'Amanda Huggenkiss', 'Justin Thyme'];
emp.length = 2;
console.log(emp);
Which would output as [ 'Joe King', 'Amanda Huggenkiss' ]
. This is because we set emp.length
equal to 2, meaning we only wanted the first two elements in the array.
Last updated