Property value of a String object in JavaScript

Posted by naivists on Stack Overflow See other posts from Stack Overflow or by naivists
Published on 2010-04-29T17:43:00Z Indexed on 2010/04/29 17:47 UTC
Read the original article Hit count: 242

As far as I understand, every string is an object in Javascript. Still, it "does not work" as I expect it to be:

var a="abc";  //here we get a new string object
a.b = 123;    //I seem to declare a property "b" of that object
alert(a.b);   //alerts "undefined"

However, if I try to define a string in the "wrong way", everything works as expected

var a=new String("abc"); //
a.b = 123;
alert(a.b);    //alerts "123"

Why is that so?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about string