Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects The Object Literal

zabi kadamzadeh
zabi kadamzadeh
5,745 Points

JS object properties/keys

Hi

I've been doing some reading on objects and other sources seem to represent the key/property as a string. i.e. var person { "name": "Sarah", }

2 Answers

Ryan Zimmerman
Ryan Zimmerman
3,854 Points

So proper JSON format is to wrap all properties and keys in quotations. This is because JSON wants to travel as basically a long string across the internet.

Technically I believe all keys are always STRINGS except for the possibility of a SYMBOL, you just don't always have to wrap them in quotations.

You need to surround the key with quotation marks if it would not be a valid identifier in JavaScript, i. e. you could not name a variable like this. One example would be a key containing spaces. There is an exception, though: You can use keywords like while or break as keys in an object, even though you can't name a variable while or break. Usually, you want to avoid this, however.

{
    while: 'weird',
    do: 'nothing',
    'once normal': 'work'
}