Arrays and Objects

Data Type: Arrays

//Defining Arrays

var languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"];

//Looping thru’ array and getting each item

for (var i=0;i<languages.length;i++)




Data Type : Object

Objects provide us with a way to represent real-world or virtual things. We can do this by storing information inside the object's properties. There are two basic ways to make objects:

Literal Notation, where we use

var Name = { };`

Constructor Notation, where we use the keyword new.

Declaration : var myObj={};

Multiple ways of Creating  Objects :

var myObj = {property1: value, property2:value};

var myObj=new Object();

Two ways of accessing Properties:

1. ObjectName.PropertyName

2. ObjectName[“PropertyName”]

Using methods

   var matt= new Object();”Matt Davingly”;


matt.setAge= function(newAge)


   this.age = newAge;



Constructors are a way to make objects with the keyword new. The most basic constructor is the Object constructor, which will make an object with no methods or properties.

For more complicated objects we can make our own constructors and put in whatever properties and methods we want.

Custom Constructors

function Person(name,age) { = name;

  this.age = age;


var bob = new Person("Bob Smith", 30);

Constructors with Methods

function Rectangle(length, width) {

  this.length = length;

  this.width = width;

  this.calcArea = function() {

      return this.length * this.width;




      return 2*length+2*width;



var rex = new Rectangle(7,3);

var area = rex.calcArea();

var perimeter = rex.calcPerimeter();

Array of Objects

Array of Objects can be made as below:-

// Our person constructor

function Person (name, age) { = name;

    this.age = age;


// Now we can make an array of people

var family = new Array();

family[0] = new Person("alice", 40);

family[1] = new Person("bob", 42);

family[2] = new Person("michelle", 8);

Loop through the Array of Objects

function Person(name,age)




// Now we can make an array of people


family.push(new Person("alice", 40));

family.push(new Person("bob", 42));

family.push(new Person("michelle", 8));

family.push(new Person("timmy", 6));

// loop through our new array

for (var i=0; i<family.length; i++)


    console.log (family[i].name);


Passing Objects into Functions

// Our person constructor

function Person (name, age) { = name;

    this.age = age;


// We can make a function which takes persons as arguments

// This one computes the difference in ages between two people

var ageDifference = function(person1, person2) {

    return person1.age - person2.age;


var alice = new Person("Alice", 30);

var billy = new Person("Billy", 25);

// get the difference in age between alice and billy using our function

var diff = ageDifference(alice, billy);

List all Properties of an Object using for Loop

var nyc = {

    fullName: "New York City",

    mayor: "Michael Bloomberg",

    population: 8000000,

    boroughs: 5


// loop to print the value of nyc's properties

for(var property in nyc) {



Extending the “Prototype”

if you want to add a method to a class such that all members of the class can use it, we use the following syntax to extend the prototype:

className.prototype.newMethod =

function() {

Inheritance in Class

In object-oriented programming, inheritance allows one class to see and use the methods and properties of another class.

//Animal class created here

function Animal(name, numLegs){;



// sayName method for Animal is created as a prototype

//[] inherits the name property from Animal Class


console.log("Hi my name is "+ []);


// Testing the constructor and method

var penguin = new Animal("Captain Cook", 2);


The power of inheritance can be further explored here:

// This is an Animal class with sayName method

function Animal(name, numLegs) { = name;

    this.numLegs = numLegs;


Animal.prototype.sayName = function() {

    console.log("Hi my name is " +;


// defined a Penguin class with defined # of legs

function Penguin(name){;



// set its prototype to be a new instance of Animal

Penguin.prototype=new Animal();

penguin=new Penguin("Moby Dick");

//Now, inheritance is in action where penguin

//inherits sayName method from Animal class


Using typeof variable

To execute something based on the type of variable, use “typeof” :

//Define object

var languages = {

    english: "Hello!",

    french: "Bonjour!",

    notALanguage: 4,

    spanish: "Hola!"


// print hello in the 3 different languages

for (var hello in languages){

    if (typeof languages[hello]==="string"){

        console.log (languages[hello]);