0

I'm confusing whether quotation marks need or not?

If I will make an object array without quotation marks like this :

var list = {
    name : 'robert',
    age : 21
};

Can I use quotation marks at key name? I can see some of guys using quotation marks on key name..

var list = {
    "name" : 'robert',
    "age" : 21
};

This is very important for me even though I learn now javascript just beginner I have to make sure that quotation marks as to where I should put it on.

Richard
  • 351
  • 4
  • 17
  • https://stackoverflow.com/questions/4348478/what-is-the-difference-between-object-keys-with-quotes-and-without-quotes – hugomarisco Apr 04 '18 at 13:45

2 Answers2

3

Both the ways we can create object array. Suppose your object property name is like powers.0 then the below statement is not valid. It will through a syntax error

var test={powers.0:"something"}

If our field name contains any special characters like dot, space ..etc we should enclosed it with a quotation mark. so the above statement can rewrite as below.

 var test={"powers.0":"something"}
Libin C Jacob
  • 1,108
  • 12
  • 34
1

Precisely an object is made up of some key value pairs. It is always a best practice to define key with quotes. If you don't do, it won't give you any error until that key is not a reserved key. you can see the reserved keys here https://mathiasbynens.be/notes/reserved-keywords.