8

Possible Duplicate:
Difference between the javascript String Type and String Object?

Write this simple code in Firebug:

console.log(new String("string instance"));
console.log("string instance");

What you see is:

enter image description here

Why these two console.log() calls result in different output? Why string literal is not the same as creating a string through String object? Is it a Firebug representation style? Or do they differ in nature?

Community
  • 1
  • 1
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
  • Please visit the post http://stackoverflow.com/questions/2051833/difference-between-the-javascript-string-type-and-string-object – Ravi Vanapalli Dec 28 '11 at 15:07
  • 2
    @TomalakGeret'kal duplicates don't warrant downvotes, just vote to close as duplicate if you have the rep, or just point it out and someone else will. – Davy8 Dec 28 '11 at 15:50
  • @Davy8: Yes they do. It's the most obvious indication of "no research effort". I closevoted _and_ downvoted, as is usual. Thanks for your input. – Lightness Races in Orbit Dec 28 '11 at 16:16

4 Answers4

7

They differ. A string literal is a primitive value, while a "String" instance is an object. The primitive string type is promoted automatically to a String object when necessary.

Similarly, there are numeric primitives and "Number" instances, and boolean primitives and "Boolean" instances.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 1
    For what it's worth, to get a string literal from a String object, you can call the `toString()` function on the String object. For example, `new String("bork") === "bork"` is false, while `new String("bork").toString() === "bork"` is true. Oh, javascript... – Jon Carter Jan 13 '14 at 15:33
  • And how does this "promotion" occur? – JohnMerlino Aug 30 '14 at 18:37
2

new String("...") returns a string object.

"..." returns a string primitive.

Some differences are:

  • new String("foo") === new String("foo") - false; object reference equality rules
  • "foo" === "foo" - true; string equality rules

and:

  • new String("foo") instanceof Object - true; it's an object derived from Object
  • "foo" instanceof Object - false; it's not an object but a primitive value

Most of the times you just want a primitive value because of these "quirks". Note that string objects are converted into primitive strings automatically when adding them, calling String.prototype functions on them etc.

More information in the specs.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
1

console.log("string instance"); prints string litral but console.log(new String("string instance")); is object so its printing all the details of string like each index and character. Watch out the screen shot below, its showing each character of "string instance".

enter image description here

dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
1

try console.log((new String("string instance")).toString())

Anyways, it's because new String("string instance") is an Object, and console.log doesn't automatically stringify objects

P Varga
  • 19,174
  • 12
  • 70
  • 108