What's the point of new String("x") in JavaScript? What's the point of new String("x") in JavaScript? javascript javascript

What's the point of new String("x") in JavaScript?


There's very little practical use for String objects as created by new String("foo"). The only advantage a String object has over a primitive string value is that as an object it can store properties:

var str = "foo";str.prop = "bar";alert(str.prop); // undefinedvar str = new String("foo");str.prop = "bar";alert(str.prop); // "bar"

If you're unsure of what values can be passed to your code then I would suggest you have larger problems in your project. No native JavaScript object, major library or DOM method that returns a string will return a String object rather than a string value. However, if you want to be absolutely sure you have a string value rather than a String object, you can convert it as follows:

var str = new String("foo");str = "" + str;

If the value you're checking could be any object, your options are as follows:

  1. Don't worry about String objects and just use typeof. This would be my recommendation.

    typeof str == "string".

  2. Use instanceof as well as typeof. This usually works but has the disadvantage of returning a false negative for a String object created in another window.

    typeof str == "string" || str instanceof String

  3. Use duck typing. Check for the existence of one or more String-specific methods, such as substring() or toLowerCase(). This is clearly imprecise, since it will return a false positive for an object that happens to have a method with the name you're checking, but it will be good enough in most cases.

    typeof str == "string" || typeof str.substring == "function"


Javascript creators created wrappers for basic types like string or int just to make it similar to java. Unfortunately, if someome makes new String("x") the type of the element will be "object" and not "string".

var j = new String("x");j === "x"  //falsej == "x" //true


String objects can have properties, while string primitives can not:

var aStringObject=new String("I'm a String object");var aStringPrimitive="I'm a string primitive";aStringObject.foo="bar";console.log(aStringObject.foo); //--> baraStringPrimitive.foo="bar";console.log(aStringPrimitive.foo); //--> undefined

And String objects can be inherited from, while string primitives can not:

var foo=Object.create(aStringObject);var bar=Object.create(aStringPrimitive); //--> throws a TypeError

String objects are can only be equal to themselves, not other String objects with the same value, while primitives with the same value are considered equal:

var aStringObject=new String("I'm a String object");var anotherStringObject=new String("I'm a String object");console.log(aStringObject==anotherStringObject); //--> falsevar aStringPrimitive="I'm a string primitive";var anotherStringPrimitive="I'm a string primitive";console.log(aStringPrimitive==anotherStringPrimitive); //--> true

You could implement overloading-like behavior:

function overloadedLikeFunction(anArgument){    if(anArgument instanceof String){        //do something with a String object    }    else if(typeof anArgument=="string"){        //do something with a string primitive    }}

Or specify argument purpose:

function aConstructorWithOptionalArugments(){    this.stringObjectProperty=new String("Default stringObjectProperty value");    this.stringPrimitiveProperty="Default stringPrimitiveProperty value";    for(var argument==0;argument<arguments.length;argument++){        if(arguments[argument] instanceof String)            this.stringObjectProperty=arguments[argument];        if(typeof arguments[argument]=="string")            this.stringPrimitiveProperty=arguments[argument];    }}

Or track objects:

var defaultStringValue=new String("default value");var stringValue=defaultStringValue;var input=document.getElementById("textinput") //assumes there is an text <input> element with id equal to "textinput"input.value=defaultStringValue;input.onkeypress=function(){    stringValue=new String(this.value);}function hasInputValueChanged(){    //Returns true even if the user has entered "default value" in the <input>    return stringValue!=defaultStringValue;}

The existence of String objects and string primitives effectively gives you two string "types" in Javascript with different behaviors and, consequently, uses. This goes for Boolean and Number objects and their respective primitives too.

Beware, however, of passing string (or other) primitives as the value of this when using the function methods bind(), call() and apply(), as the value will be converted to a String object (or a Boolean or a Number object, depending on the primitive) before being used as this:

function logTypeofThis(){    console.log(typeof this);}var aStringPrimitive="I'm a string primitive";var alsoLogTypeofThis=logTypeofThis.bind(aStringPrimitive);console.log(typeof aStringPrimitive); //--> string;logTypeofThis.call(aStringPrimitive); //--> object;logTypeofThis.apply(aStringPrimitive); //--> object;alsoLogTypeofThis(); //--> object;

And unexpected/counter-intuitive return types:

var aStringObject=new String("I'm a String object");console.log(typeof aStringObject); //--> objectaStringObject=aStringObject.toUpperCase();console.log(typeof aStringObject); //--> string