Press key to advance. Zoom in/out: Ctrl or Command + +/-.

Basic JavaScript
Part 2: Arrays

Estelle Weyl


var browsers = ['Safari', 'Chrome', 'Explorer', 'FireFox', 'Opera'];

var random = ['shoe',27,null,[42,17,19],Math.PI];

Easier to read

var heroes = [ 'Anita Borg',
               'Grace Hopper',
               'Ada Lovelace',
               'Emmy Noether'];

Old school (with constructor)

var browsers = new Array('Safari', 'Chrome', 'Explorer', 'FireFox', 'Opera');

Arrays → zero indexed

Use brackets to retrieve values based on index


First Item


Last Item

heroes[heroes.length - 1];

Add item

heroes[heroes.length] = 'Marie Curie';

Arrays are Objects

Arrays are Objects

Arrays are loosely typed

Like all objects, arrays have properties

var arrayItemCount = myArrayName.length;

Like all objects, arrays have methods


Like all Objects, Arrays have built in functions, or "methods" they inherit from the Object object.

Mutator Methods

Removes the last element from an array and returns that element.
Adds one or more elements to the end of an array and returns the new length of the array.
Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
Removes the first element from an array and returns that element.
Sorts the elements of an array.
Adds and/or removes elements from an array.
Adds one or more elements to the front of an array and returns the new length of the array.

Accessor Methods

Returns a new array comprised of this array joined with other array(s) and/or value(s).
Joins all elements of an array into a string.
Extracts a section of an array and returns a new array.

Array Methods: Adding Items

heroes[heroes.length] = 'Hedy Lamar';

Add to end:

 heroes.push('Barbara Liskov');
 heroes.push('Fran Allen', 'Valerie Aurora');

Add to beginning

heroes.unshift('Desi', 'Nola');

Adding in the middle (based on index)

heroes.splice(2,0,'Sarah', 'Adele');
Note: 0 is # to delete

Array Methods: Deleting Items

Delete last item

var lastItem = heroes.pop();

Delete first item

var firstItem = heroes.shift();

Delete based on index

var deletedItem = heroes.splice(2,1);


  • Create an array with 6 items.
  • Remove the 5th item.
  • Make the 5th item the first item.
  • Remove the last item.
  • Make the last item the 2nd item.
  • Remove the 3rd and 4th items. Make them the last two items.
  • What is the value of the 3rd item now?

Heroes Cheatsheet

  • Anita Borg
  • Grace Hopper
  • Barbara Liskov, Fran Allen (Turing Award winners)
  • Emmy Noether (nobel prize in Math, tutored Einstein)
  • Adele Goldberg (one of the developers of SmallTalk)
  • Ada Lovelace
  • Marie Curie
  • Hedy Lamar (holds a patent wireless networking feature proposed for encoding messages during WWII)
  • Valerie Aurora (Linux kernel hacker)
  • Kimberly Bryant (Black Girls Code)
  • Desi Macadam, Nola Stowe (DevChix)
  • Sarah Allen (Came up with this list)


  • Everything outside of primitive types are objects
  • Objects are mutable
  • Objects are a container of properties (key/value pairs)


Creating objects with object literal syntax.

var class = {
    teacher: "Estelle",
    subject: "JavaScript",
    hours: 6

Accessing/setting Object properties.

class.teacher = "Estelle"
class[subject] = "JavaScript"
class.hours = 6

document and window

When setting variables in global scope, they will be set on the window object

var cat = "Sassafrass";

When retrieving DOM nodes from a web page, you will be accessing document objects

Find a node, then access the objects properties

var counter = document.getElementById('presentation-counter');