Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The String() constructor function
takes one parameter: the string value being created. Below, we create a
variable, stringObject, to contain the
string value 'foo'.
<!DOCTYPE html><html lang="en"><body><script> // create string object var stringObject = new String('foo'); console.log(stringObject); // logs 'foo {0="f", 1="o", 2="o"}' </script></body></html>
Instances from the String()
constructor, when used with the new
keyword, produce an actual complex object. You should avoid doing this
(using literal/primitive numbers) due to the potential problems
associated with the typeof operator.
The typeof operator reports complex
string objects as 'object' instead of the primitive
label ('string') you might expect. Additionally, the
literal/primitive value is just faster to write and is more
concise.