Here's a mini FAQ for you.
Why are there String and Number objects?
Primitive values, like "foobar" or 123 cannot have properties and methods, so there must be objects that actually hold these.
How String and Number objects are used?
They're used implicitly by the engine itself. Every time you assess a property of a primitive, the engine creates a new implicit object just for this (this is called "autoboxing") . For example x = "foobar".length is turned into this:
temp = new String("foobar")
x = temp.length
delete temp
Can I use String and Number objects in my own code?
You can, but hardly need. Due to automatic boxing, you can call all objects' props and methods directly on primitive values. Also, primitives and boxed objects behave differently under some circumstances, e.g. if(x)... vs. if(new String(x))....
What are String() and Number() functions?
These are constructors for String and Number objects. Additionally, when called directly, without new, they perform type conversion, i.e. return a new primitive (not object) of their respective type. In pseudocode, the logic behind String is like this:
function String(primitiveValue) {
primitiveString = convert_to_string(primitiveValue)
if called with new
return new StringObject(primitiveString)
else
return primitiveString
Reference: http://es5.github.io/#x15.5.1
Are direct calls to String() and Number() useful?
Yes! Every time a type casting is required you want to use these. Some people prefer hacks, like +x instead of Number(x) or x+"" instead of String(x), but explicit casting looks cleaner.
Why typeof new String() is object and not string?
Everything created by the means of new is an object, no matter which specific constructor is used.
newoperator withString(),Number()orBoolean().