Accessing Array Values

Accessing Arrays Values

  • Values in an array are accessed as if they are in a numbered list

  • Arrays are zero based meaning the 1st “value” in an array is at position (index) 0, the 2nd element is at position 1, and so on

  • You access the value of an element in the array by passing the index of the item in square brackets


Example

  • You can access a value of an array by referencing its index (i.e. its order within the array)
// declare a variable called chipmunks and use it to store
// an array of names
const chipmunks = ["Alvin", "Simon", "Theodore"]

// reference the first value in the array
// here we pass in a zero, since that represents the first
// element (value) in an array

const bandMember = chipmunks[0] //> the value stored in bandMember is "Alvin"

JS Bin on jsbin.com