I'm answering this because there is a lot of misinformation in the subject:
Is that a String or an Object?
No "cat" is a primitive String value:
typeof "cat"; // "string", a String value
"cat" instanceof String; // false
typeof new String("cat"); // "object", a String object
new String("cat") instanceof String; // true
I will talk later on about types and primitive values.
Does it inherits all properties/methods from String or String.prototype?
Well, when you use the property accessor operator (the dot or the bracket notation), the primitive value is implicitly converted to object, internally, therefore all the methods on String.prototype are available, for example:
When you access:
"cat".chatAt(0);
Behind the scenes "cat" is converted to object:
Object("cat").chatAt(0);
That's why you have access to all the inherited properties on values.
Why is there a String and String.prototype?
String is a constructor function, allows you to create String objects or do type conversion:
var stringObj = new String("foo"); // String object
// Type conversion
var myObj = { toString: function () { return "foo!"; } };
alert(String(myObj)); // "foo!"
The String.prototype object, is the object where String object instances inherit from.
I know it's confusing, we have String values and String objects, but most of the time you actually work only with string values, don't worry for now about String objects.
Should I call String the String object and String.prototype the String prototype?
You should call String "The String constructor".
"String prototype" is ok.
You should know that "Everything is NOT an object".
Let's talk about types, there are five language types specified:
- String
- Number
- Boolean
- Null
- Undefined
A primitive value is " a datum that is represented directly at the lowest level of the language implementation", the simplest piece of information you can have.
The values of the previously described types can be:
- Null: The value
null.
- Undefined: The value
undefined.
- Number: All numbers, such as
0, 3.1416, 1000, etc.. Also NaN, and Infinity.
- Boolean: The values
true and false.
- String: Every string, such as
"cat" and "bar".