Rules for unquoted JavaScript Object Literal Keys?
Asked Answered
G

6

14

In JavaScript, you can define an object like this:

var d = {1: 'test'};

and I can set a key with a negative number index like this:

d[-1] = 'test2';

but if I try to use a negative number in the literal initialization, I get an error:

var d = {1: 'test', -1: 'test2'};
Uncaught SyntaxError: Unexpected token -

Why is this? Why can't I use a literal negative number as a key to an object? Is there a workaround that allows me to initialize it as a literal. I know I could use strings instead, but I want to use integers.

Glutelin answered 20/2, 2012 at 19:56 Comment(1)
use quotes around property name like '-1':'test2'Chameleon
F
18

From Unquoted property names / object keys in JavaScript, my write-up on the subject:

Quotes can only be omitted if the property name is a numeric literal or a valid identifier name.

[…]

Bracket notation can safely be used for all property names.

[…]

Dot notation can only be used when the property name is a valid identifier name.

-1 is not a numeric literal, it’s a unary - operator followed by a numeric literal (1).

I also made a tool that will tell you if any given property name can be used without quotes and/or with dot notation. Try it at mothereff.in/js-properties.

Screenshot

Freedafreedman answered 5/3, 2012 at 17:52 Comment(5)
I think you meant "unary - operator."Rummage
Your tool claims that -1 and +1 can be used as unquoted property names, even though they don't actually work.Honna
@StuartCook Thanks for the report! Fixed: mothereff.in/js-properties#-1 (You might need to refresh to update the appcache.)Freedafreedman
@MathiasBynens, Are there any limits if the key is quoted?Mcmorris
@Mcmorris As long as you use a valid string, there are no such limits at the language-level, no.Freedafreedman
R
4

Interesting question.

The thing is, there's no difference between typing

var d = {24: 'foo'};

and

var d = {"24": 'foo'};

You can verify this by doing:

var d = {24:'foo', "24":'bar'};

Notice that it only has one "24" property (and fails in strict mode).

So while this doesn't explain why you can't do -1 without quotes, hopefully it does explain that "-1" is just as good.

Interestingly, unquoted fractional numbers seem to work fine.

Edit: Felix Kling explains why it doesn't work in a comment on another answer. -1 isn't a numeric literal, it's an expression with a numeric literal and a unary - operator -- therefore it's not suitable as an object key.

Rummage answered 20/2, 2012 at 20:1 Comment(0)
M
3

Its because -1 isn't a valid variable identifier.

Monzon answered 20/2, 2012 at 19:57 Comment(4)
But I can still assign it by key later, so is this a limitation of the literal syntax that keys have to be valid variable names?Glutelin
Correct. its just a representation of hte object.Monzon
@jterrace, @Daniel: Property names in object initializers can be string literals and numeric literals as well (not only identifier names). But apparently, -1 is not a numeric literal: es5.github.com/#x7.8.3Disposal
@FelixKling aha, you're right, it's a numeric literal with a unary operator prepended, of course!Rummage
F
3

Change to

var d = { 1: 'test', '-1': 'test2'};

Identifiers, that are keywords or can not be a normal identifier for other reasons, can still be used if surrounded by quotes.

Fakery answered 20/2, 2012 at 19:59 Comment(1)
1 cannot be used as identifier either... just saying ;)Disposal
D
1

I'm not sure exactly why, but you have to quote the negative number in the object initializer:

var d = {1: 'test', '-1': 'test2'};

All object keys are treated as strings internally, so you can address this key using either:

d[-1] or d['-1']
Dynamite answered 20/2, 2012 at 19:59 Comment(0)
L
1

When the key contains certain characters, you have to put it in quotes like this:

var d = {"1": 'test', "-1": 'test2'};

Here's a pretty good reference on what characters are allowed without the quoting and it has a specific section about object property declaration too: http://asenbozhilov.com/articles/identifiers-en.html#identifier_start.

Londalondon answered 20/2, 2012 at 19:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.