DangerMouse5
DangerMouse5

Reputation: 45

Why can’t I have a global variable named “name”?

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"};

console.log(car.type);
console.log(name.first);

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

Upvotes: 3

Views: 70

Answers (1)

Shajith Kunnumbrath
Shajith Kunnumbrath

Reputation: 86

"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"};

Upvotes: 1

Related Questions