6

Javascript objects' properties can be named with the empty string, for example:

foo = { 
    "" : "bar"
}

Dot notation does not seem to be able to call this property. Console output:

foo.
>> "missing name after . operator"

How would you call the "" property?

3
  • 1
    Same way you'd access a property that contains anything else that wouldn't be legal JavaScript syntax. Commented Jun 1, 2015 at 20:40
  • "An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string." developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/… Commented Jun 1, 2015 at 20:42
  • I'm not sure if that comment is directed at me; a valid JavaScript string isn't necessarily valid JavaScript code, which is what the dot notation is. Commented Jun 1, 2015 at 20:43

2 Answers 2

5

Use an empty string as a key with the bracket syntax:

foo[""]
Sign up to request clarification or add additional context in comments.

Comments

0

This can be accomplished with square bracket notation. Console output:

foo[""]
>> "bar"

Documentation

4 Comments

Anyone know if it's possible with dot notation?
@meetalexjohnson—no. Dot notation can only be used with property names that are valid identifiers (e.g. something that could be used as a variable name), and "" doesn't fit the criteria.
@meetalexjohnson Wait, why did you comment on your own answer?
This actually isn't the only type of object key that can't be expressed with dot notation. foo["Now this is a story all about how my life got flipped turned upside down"] = bar; is valid, and you can similarly only access it through square bracket notation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.