JS-152-Objects-Arrays
  • Introduction
  • 04-Objects
    • OOP-Basics
      • Overview
      • Coding the Example
    • Prototypes
      • Object Prototypes
      • Prototypes Continued
    • Inheritance
      • Inheritance
    • Synopsis
      • Synopsis
    • Challenges
    • Solutions
      • Constructor Function
        • Person Constructor
        • Explanation
      • Inheritance Challenge
        • Code
        • Explanation
      • Family Challenge
        • Child
        • Pet
  • 05-Arrays
    • Array Basics
      • Array Overview
      • Creating an Array
      • Individual Elements and Length
      • Iterating Over Arrays
    • Array Methods
      • Methods Overview
      • .join() Method
      • .reverse() Method
      • .split() Method
      • .replace() Method
      • .splice() Method
      • .map() Method
      • .indexOf() Method
      • .filter() Method
      • .every() Method
    • Palindrome Algorithm
    • Array Challenges
    • Solutions
      • First and Last
        • First Element
        • Last Element
      • Most Frequent
      • Largest and Smallest
        • Single Array
        • Multiple Arrays
      • Missing Number
      • "Arrays" in the DOM
      • Sorting
        • Bubble Sort
        • Selection Sort
    • Resources
Powered by GitBook
On this page
  • Individual Elements
  • Length
  1. 05-Arrays
  2. Array Basics

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.

PreviousCreating an ArrayNextIterating Over Arrays

Last updated 7 years ago