2

Ok, so this might be a super dumb question but I was doing some review on objects in js and I saw the w3 schools example for it and I tried doing my own. Didn't work, just printed undefined. Copied the w3 schools right over and it did work. Changed the names and it didn't. I asked one of my good coder friends and he could not figure it out either. Here's the code I'm trying:

 var car = {type:"Fiat", model:"500", color:"white"};
 var name = {first:"Owen", last:"Donnelly"};

When I do car.type it prints Fiat but when I type name.first it says undefined.

  • 3
    What printed undefined? did you console.log(car, name) ? – SteveLacy Apr 21 '16 at 21:33
  • 2
    That simply declares two objects. What's the problem? Please post the code that's causing you the problem, clearly highlighting the error and where it occurs. – ManoDestra Apr 21 '16 at 21:33
  • could this be a duplicate of [Chrome/Firefox console.log always appends a line saying undefined](http://stackoverflow.com/q/14633968/1048572)? – Bergi Apr 21 '16 at 21:38

1 Answers1

2

"name" is a reserved keyword in javascript so you cannot use it as variable identifier. instead you can use it like this as below:

var Name = {first:"Owen", last:"Donnelly"};
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • 2
    Not true. `name` is a *global variable* which is reserved for the window in the browser but it's not a reserved keyword. – Mike Cluck Apr 21 '16 at 21:45
  • Would be better to use lower case for that object's first letter. It would also be better to have a more meaningful name too, such as customerName, etc. http://javascript.crockford.com/code.html – ManoDestra Apr 22 '16 at 13:17