What is the difference between "String (1)" and "new String (1)" in Javascript?

8

Why in Javascript is it possible to call String as a function and also instantiating?

See the example:

var str1 = new String(1)
var str2 = String(1)

console.log("Valor é %s e o tipo é %s", str1, typeof(str1));
console.log("Valor é %s e o tipo é %s", str2, typeof(str2));

I noticed that there is a small difference between one and the other, and it confused me a bit.

In the first result new String is given as object by typeof . E String() is given as string .

But in practice, is there a difference between the two? I realize that they also have the same methods.

See:

var a = new String(1);
var b = String(2);

console.log(a.concat);
console.log(b.concat);

console.log(a.substr);
console.log(b.substr);

Note : We do not need to focus only on String , since this is true for calls of Number , Boolean and Array .     

asked by anonymous 28.11.2016 / 14:29

2 answers

9

Primitive types in JavaScript allow you to convert something to primitive or an object using new to create a new primitive type . Apparently they are the same because we can do:

String(1) == new String(1) // que dá true

But in this case what happens is a conversion between types and at the end of some internal steps the comparison is "1" == "1" . But doing the comparison with === , which compares the types too:

String(1) === new String(1) // dá false!

In fact String(1) gives a primitive of type String and new String(1) gives an object, an instance, which when asked for its value returns a primitive.

That is:

String(1) === new String(1).valueOf() // true

For the example it is indifferent if we pass numbers or text, both are converted to string , so I'll use an example to explain it better:

const texto = 'Olá!';
const TypeString = String(texto);
const StringInstance = new String(texto);
console.log(typeof TypeString, typeof StringInstance); // string, object
console.log(TypeString, StringInstance); // Olá!, String {0: "O", 1: "l", 2: "á", 3: "!", length: 4, [[PrimitiveValue]]: "Olá!"}

TypeString.autor = 'Maria';
StringInstance.autor = 'Maria';
console.log(TypeString.autor, StringInstance.autor); // undefined, "Maria"

In the example above we can see more differences. One of them is that a primitive type , that is a string text can not have more properties, TypeString.autor does not save anything. But an instance, since it is an object can, and then StringInstance.autor = 'Maria'; is stored in objeto .

This behavior is the same for primitives , ie Number , Boolean , String .

    
28.11.2016 / 16:00
8

A basic difference is that String(1) works as type by value and new String(1) works as a type by reference, since it is an object. This makes a difference, because if you have two strings of the same value, they are the same but have two objects that happen to have the same value, they are different.

In JavaScript Strings are not objects, it is a primitive type, but they can be converted to objects when they need them and then the appropriate prototype methods can be accessed.

The general recommendation is to use only String .

console.log(new String(1) === new String(1));
console.log(String(1) === String(1));
console.log("1" === "1");

If I remember other differences I put here.

    
28.11.2016 / 15:01