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

Asked

Viewed 502 times

11

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

Take 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 little.

In the first result new String is given as object for typeof. And String() is given as string.

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

Behold:

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);

Observing: We don’t need to just focus on String, since the same occurs with the calls of Number, Boolean and Array.

2 answers

11


The primitive types in Javascript allow you to convert something into primitive value or an object using new to create a new instance of primitive type. Apparently they’re 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 gets "1" == "1". But making the comparison with ===, that compares the guys also:

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

because in fact String(1) gives a primitive of the type String and new String(1) gives an object, an instance, that when we ask for its value returns a primitive.

That is to say:

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

For example it is indifferent whether we pass numbers or text, both are converted to string, so I’ll use an example to explain 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, namely a string text can no longer have properties, TypeString.autor does not guard anything. But an intuition, since it is an object can, and there StringInstance.autor = 'Maria'; is stored in the objeto.

This behavior is the same for primitive, that is to say Number, Boolean, String.

  • It would be the same case as int and Integer in Java

  • @Guilhermelautert exactly that.

  • 1

    Primitives have no properties but pretend to have methods: console.log(TypeString.replace('á', 'é')); // olé

8

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

Javascript Strings are not objects, it is a primitive type, but they can be converted to objects when needed and then the methods of the appropriate prototype 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");

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.