Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What are the use cases for doing new String("already a string")?

What's the whole point of it?

like image 473
Pacerier Avatar asked Apr 21 '11 22:04

Pacerier


People also ask

What is the use of new string in JavaScript?

String() constructor The String constructor is used to create a new String object. When called instead as a function, it performs type conversion to a primitive string, which is usually more useful.

What is the purpose of new string?

Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string. Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

What is the difference between creating string as new () and literal?

When we create a String object using the new() operator, it always creates a new object in heap memory. On the other hand, if we create an object using String literal syntax e.g. “Baeldung”, it may return an existing object from the String pool, if it already exists.

What is difference between string and new string?

String Literals e.g. String str="java"; (we use only double Quotes) are different from String Object (we use new keyword) e.g. String str=new String("java"); String is Immutable Object i.e. If value changes a new Object is created and returned to you eg See replace() and replaceAll() functions and many more.


2 Answers

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); // undefined  var 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"

like image 163
Tim Down Avatar answered Sep 22 '22 19:09

Tim Down


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"  //false j == "x" //true 
like image 38
Lucia Avatar answered Sep 25 '22 19:09

Lucia