6. We Made A Friend! Solved it but still need to understand some things



I'm glad I've managed to solve this on my own without looking at people's solutions (As you could do when you get stuck :slight_smile: ). But I'm not sure I'm confident with the result nor I understand all parts:
I've tried first by declaring the new object (contact) like this var newContact and it was accepted. Then I tried without var and it was accepted too. Can you tell me why it is like this?

The other part I don't understand, not just in this lesson, is the semicolons after the closing curly braces.. I haven't grasped yet when they're needed. As you can see in my code, I didn't use any and it was accepted. I undestand how they're needed after each single statement inside functions and in declarations though. Is it just because of how forgiving Javascript is? Thanks in advance.

var add = function(firstName, lastName, email, phoneNumber) {
    newContact = {
        firstName: firstName,
        lastName: lastName,
        email: email,
        phoneNumber: phoneNumber
    contacts[contacts.length] = newContact;
add("John", "Doe", "john.doe@example.com", "(650) 555 5555");


First Question

it gives different syntaxes for variable declaration:

var x = 1; //  This can be used to declare local and global variables(scope). 
x = 1; // Without var it is important that you assign a value, here we have a var in global scope and you get a strict Javascript Warning
x; // Doesnt work ;)

Objects are variables too, so the shown rules work here again:)

Here a good link

There you can read much about variables

Second Question

A semicolon at the end of a curly brace generates an additional empty statement . This is nothing bad in and of itself and does not lead to an error message , but used a full cycle , to do nothing. That means a semicolon after a curly brace is not needed.


Thank you! Although we've learned about it, I was thinking 'objects' instead of making the connection between them and variables. Many things in the linked article are familiar and we've learned about them but they still confuse me. I need to practice more :slight_smile:

Good to know about the use of semicolons.. I'll try to remember that. Thanks.