Listing Everybody


The instructions state:

At the start of the function, define a variable to store the number of items in the contacts array. Call it contactsLength.

This line of code would look something like this:

var contactsLength = contacts.length

Why is this necessary? When I create my for loop within the function I can just simply use contacts.length for the second part of the loop. Here is my code without the contactsLength variable. It works just fine. Is there an importance to the variable that I am missing?

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: ""

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: ""

var contacts = [bob, mary];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);

var list = function() {
    for (var i = 0; i < contacts.length; i++) {



its not needed, its a micro-optimization. This way, the length of the array doesn't have to be calculated on each iteration.


A post was split to a new topic: Listing everybody


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.