Is there a syntax error here?


#1
var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};
var mary= {
     firstName:"Mary";
    "lastName":"Johnson";
    "phoneNumber":"(650) 888 - 8888";
    "email":"mary.johnson@example.com";
};
var contacts=[bob,mary];
console.log(contacts);

I know i am not doing what's been asked to do, but rather, I am trying to see the "contacts" printed out. Why I am getting a syntax error here?


#2
"lastName":"Johnson";
    "phoneNumber":"(650) 888 - 8888";
    "email":"mary.johnson@example.com";

Why did you use there double quotes around lastName, phoneNumber and email ?

This should be your error


#3

@dookoo_de
thanks for the reply, but double quote actually isn't an error. but i did what you suggested and removed double quote. then i am still getting the same error.

edit- from what i have learned, double quote can also be used for properties, like..

"property":"propertyname"

#4

Hi didnt see that before and i dont know why i should type more like i must :smiley:

Unexpected Token

firstName:"Mary"; //semicolon chage to commata
"lastName":"Johnson"; //semicolon chage to commata
"phoneNumber":"(650) 888 - 8888"; //semicolon chage to commata
"email":"mary.johnson@example.com"; // remove semicolon

#5

For the record, not to interfere, quotes, either single or double, are all seen by the compiler as single unless there is a string construct in play that uses both.

More importantly, the OP has hit on something revealing, that of string composition of data keys. All property names are by design, string type data. Quotes on single terms are implicit.

firstName: "Bill",
"last name": "Jones"

Since both are already cast as strings, regardless, both of the above are valid property names, or more specifically, *identifierName*s. Quotes make it possible for us to introduce spaces and special characters that are otherwise not allowed.

In a literal sense, we won't write stuff like this, but in a dynamic world anything can happen and JS allows for that, so long as we quote it.


#6

@dookoo_de
that solves that, thanks.
the quote was actually there in the first object exercise(if i remember correctly).


#7

@mtf
i knew that quotes are used, but i didn't know the reason. i learned something new today. thank you very much.

so, can you tell me how can someone write spaces or special characters in constructor notation? Or is it okay to write it as normal like..

person.last name=jones;

#8

No. We cannot use any unusual name with spaces in dot notation. Only in subscript, or bracket notation. person['last name'] would yield Jones in this present sense.


#9

@mtf
understood, thanks.