Are string literals objects or not?
javascript
Solution
String literals are primitives (String values), String objects can be created with the String constructor in a `new` expression:
"foo" instanceof String // false
new String("foo") instanceof String // true
Edit: Something that seems to be confusing (by looking at the accepted answer here), is that you can still access properties defined on the prototype objects of primitive values, for example:
"foo".indexOf == String.prototype.indexOf // true
"foo".match == String.prototype.match // true
String.prototype.test = true;
"foo".test // true
true.toString == Boolean.prototype.toString
(3).toFixed == Number.prototype.toFixed // true
// etc...
The reason of that relies on the Property Accessors, the dot notation `.` and the bracket notation `[]`.
Let's give a look to the algorithm in the ECMA-262 specification:
The production MemberExpression : MemberExpression [ Expression ] (or MemberExpression . Identifier) is evaluated as follows:
Evaluate `MemberExpression`.
Call `GetValue(Result(1))`.
Evaluate Expression.
Call `GetValue(Result(3))`.
Call `ToObject(Result(2))`.
Call `ToString(Result(4))`.
Return a value of type Reference whose base object is Result(5) and whose property name is `Result(6)`.
In the Step 5, the `ToObject` internal operator type-converts the MemberExpression to object, depending on it's type.
The primitives are converted to Objects without noticing, and that's why you can access the properties defined on the prototype.
Problem
Trying to get my JavaSscript fundamentals strong. So the question is about string literals. Aren't they `Objects`? If your answer is 'yes' then my question is why is `instanceof` returning `false`? ``` > var s = new String > s.constructor.toString() function String() { [native code] } > typeof s object > s instanceof String true > s instanceof Object true > s instanceof Number false ``` So far so good. ``` > typeof 'c' string > 'c' instanceof Object false > 'c' instanceof String false > 'c'.length 1 > 'c'.charAt(0) c > 'c'.constructor.toString() function String() { [native code] } ```