Skip navigation

JavaScript have some odd behaviour on object property names. If you know this for first time, you will be shocked. Programmers from other language background will be surprised to know this kind of JavaScript behaviour.

// declare a variable and create a new object with property name and value raja

var myO = {name:"raja"};
alert(myO.name);  //raja

Your shocks starts here…

myO.if = "use reserved word";
alert(myO.if);  //use of reserved word

myO["if else if && || *^ "] = "you can use anything";
alert(myO["if else if && || *^ "]);  //you can use anything

myO[""] = "Surprise";  // no error. even empty is allowed
alert(myO[""]);  // Surprise

Similar article written by mathias

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: