Difference between the javascript String Type and String Object?

Strings are a value type in JS, so they can’t have any properties attached to them, no prototype, etc. Any attempt to access a property on them is technically performing the JS [[ToObject]] conversion (in essence new String).

Easy way of distinguishing the difference is (in a browser)

a = "foo"
a.b = "bar"
alert("a.b = " + a.b); //Undefined

A = new String("foo");
A.b = "bar";
alert("A.b = " + A.b); // bar

Additionally while

"foo" == new String("foo")

is true, it is only true due to the implicit type conversions of the == operator

"foo" === new String("foo")

will fail.

Leave a Comment