javascript - Can I use an empty string as an object identifier? -


i've been tinkering objects , seemingly can have '' (an empty string) property name, so:

o = {     '':    'hello',     1:     'world',     'abc': ':-)', }; console.log(o['']); 

seems work fine, i'm curious know, is valid? i've poked @ ecma specs , asked our ever-knowledgeable friend google variations of question , conclusion i don't know.

my sources

http://www.jibbering.com/faq/faq_notes/square_brackets.html

yes, technically totally valid , can safely use it. object key needs "string", not exclude empty string.

if convinient , useful story.

see should use empty property key?


since 'empty string' 1 of falsy values in ecmascript, consider following example:

var foo = {     ':-)': 'face',     'answer': 42,     '': 'empty' };  object.keys( foo ).foreach(function( key ) {     if( key ) {         console.log(key);     } }); 

that snippet log :-) , answer. 1 pitfall doing this.


Comments

Popular posts from this blog

php - Wordpress: Link a post to posts of another category manually -

delphi xe7 upload/download dropbox file using the rest client and show progress -

java - Get more than One value and display it -