Tag: Arrays

Practice javaScript Objects in an array

Here I wrote an object constructor called person, and then I created an empty array, then I am adding each family members to it one by one and the looping true them to print their name.  I hope this comes handy to you.

 


// My Person constructor
function Person(name,age){
this.name = name;
this.age = age;
};

// Now I 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);
family[3] = new Person("timmy", 6);

// loop through my new array

for(i=0; i<family.length; i++){
console.log(family[i].name);
}