I’ve been tinkering with objects and seemingly you can have ” (an empty string) as a property name, like so:
o = {
'': 'hello',
1: 'world',
'abc': ':-)',
};
console.log(o['']);
Seems to work just fine, however I’m curious to know, is this really valid? I’ve poked at the ECMA specs and asked our ever-knowledgeable friend Google variations of the question and my conclusion is that I don’t know.
My sources
Yes, technically its totally valid and you can safely use it. An object key needs to be a “string”, which does not exclude an empty string.
If that is convenient or even useful is another story.
See Should I use an empty property key?
Since the ’empty string’ is one of the
falsy valuesin ecmascript, consider the following example:That snippet would only log
:-)andanswer. So that is one pitfall for doing this.