"6. We made a Friend!" - How is the array size increased?


I found the answer for this but I want to understand what's going on.

function add (firstName, lastName, email, phoneNumber)
    contacts[contacts.length] = 
        firstName: firstName,
        lastName: lastName,
        email: email, 
        phoneNumber: phoneNumber 

How exactly does the contacts[contacts.length] = {the information list} increase the size of the array? how is it adding the object at the end of the array?


Array can't be increased in size (at least, that is what i learned at edx online cours cs50), array are fixed in size, if you need to create a new array (obviously you make this one bigger) and copy all the content, and add the new content to the array, or you already had a bigger then the values you put in.

It could be, that javascript makes it looks like an array, while in fact it could underneath be a linked list. This sort of question are hard to answer, you would first need understanding at a lower level (C will do nicely) and then find out how javascript implants arrays


I have had trouble with the same issue, but I'll have a stab at the reason, because I think.. I figured it out! :confused:

Quick caveat: I am a newbie to coding.

So, to add a new object to an array you follow the structure:

array[position] = {object};

Which is the same as "contacts[contacts.length] = {}; as above.

The reason we use "contacts.length" is because, if you think about it, the length of the Array is also technically the next position for a new object in the array.

So if you have 3 objects in your array, because the first object is indexed with '0' then the next 'free' spot for a new object would be array[3] which is also the array.length. If that makes sense? :smile:

So an array with array.length 'n' should always also correspond to the next free position in the array - so the function can keep on going.

Let me know if that follows!


So, this is not an array. Array's are fixed size of memory, if you need to expand the array you either need to create a new one, or under the hood array's with undefined length are linked list


Ok, now I'm super confused ha

I could have sworn I learned how to add things to an array.

In this task called "Array of Objects" I thought we learned to add new objects to an array...? Is this not 'expanding' the array?

// Our person constructor
function Person (name, age) {
this.name = 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);
// add the last family member, "timmy", who is 6 years old
family[3] = new Person("timmy",6);

I might be missing something.

Thanks for helping out :smile:


I did a little bit of C programming, there you learn that array's are fixed size, so either javascript: makes a big array, or javascript works some magic with arrays (create a new bigger array when it runs out of room), or something else. I learned that array's are fixed size, which is conflicting with what is happening here


Yeah, it must be different for javascript.

Because I just did some research and found a sight that talks about 'dynamic' arrays in javascript.


"Like all scripting languages​​, JavaScript has dynamic arrays: their size is not predetermined, nor the type of data."

I guess arrays are different for different languages? Which is an unsettling realisation :smile:


i found this on stackoverflow:
The thing about dynamic languages is, well, that they're dynamic. Just
like ArrayList in Java, or arrays in Perl, PHP, and Python, an Array in
JavaScript will allocate a certain amount of memory and when it gets to
be too big, the language automatically appends to the object. source


Ok, so with regards to the question of this thread...

The function will keep adding objects to that array until a certain point/size and then the language automatically appends to the object.


yes, that is what is happening


RIght on. :sunglasses: Thanks for clearing that up for me.