Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between String() and new String() in Javascript

In JavaScript is there any difference between using String() and new String()?

console.log(String('word')); // word

console.log(new String('word')); // word
like image 515
cham Avatar asked Apr 29 '18 00:04

cham


People also ask

What is difference between string and new string?

String(String original) : 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.

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 new string in JavaScript?

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.


2 Answers

Using the String() constructor without new gives you the string (primitive) value of the passed parameter. It's like boxing the parameter in a native object if necessary (like a Number or Boolean), and then calling .toString() on it. (Of course if you pass a plain object reference it just calls .toString() on that.)

Calling new String(something) makes a String instance object.

The results look the same via console.log() because it'll just extract the primitive string from the String instance you pass to it.

So: just plain String() returns a string primitive. new String(xyz) returns an object constructed by the String constructor.

It's rarely necessary to explicitly construct a String instance.

like image 108
Pointy Avatar answered Oct 16 '22 17:10

Pointy


String() returns a string primitive and new String() returns a Object String. This has some real consequences for your code.

  1. Using String() returns 'true' with other primitives both with == and === operator.
  2. Using String() gives you a primitive so it cannot use the "instanceOf" method to check its type. You can check only value type with "typeof" operator
  3. Using new String() with "instanceOf" method with String or Object prototypes - both assert to true.
  4. Using new String() will return 'true' with string primitives only by calling valueOf() method. String() has also this method and returns true when compared to string of the same value.
  5. Using new String() allows you to add some other properties and methods to the Object to allow more complex behaviour.

From my coding experience you should avoid using new String() if you have no need for adding special methods to your String Object.

    var x = String('word');
    console.log(typeof x); // "string"
    var y = new String('word');
    console.log(typeof y); // "object"

    // compare two objects !!!
    console.log(new String('') === new String('')) // false!!!

    // compare with string primitive
    console.log('' == String('')) // true
    console.log('' === String('')) // true

    //compare with string Object
    console.log('' == new String('')) // true

    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    console.log('' === new String('')) // false !!!!
    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    // instance of behavior
    console.log(x instanceof String); // false
    console.log(x instanceof Object); // false

    // please note that only new String() is a instanceOf Object and String
    console.log(y instanceof String); // true
    console.log(y instanceof Object); // true

    //valueOf behavior
    console.log('word' == x.valueOf()); // true
    console.log('word' === x.valueOf()); // true

    console.log('word' == y.valueOf()); // true
    console.log('word' === y.valueOf()); // true

    //create smart string
    var superString = new String('Voice')
    superString.powerful = 'POWERFUL'
    String.prototype.shout = function () {
        return `${this.powerful} ${this.toUpperCase()}`
    };

    console.log(superString.shout()) //"POWERFUL VOICE"
like image 36
twboc Avatar answered Oct 16 '22 19:10

twboc