Working with funny JSON variable names

I was working on an User Interface (UI) that played with some JSON data sitting on the page. One of the variables had a dash (-) in the name which would obviously put a syntax error on the page.   After a bit of playing, I came up with the solution. I hope it helps someone. Obviously this naming convention is through whenever working with any Javascript object.


var user = { "name": "Jamie Chung", "has-avatar": false };
alert ( ); // Jamie Chung

Broken Code

alert( user.has-avatar ); // Error on page because has-avatar is invalid

Working Code

alert ( user["has-avatar] ); // true