4

What is the difference between creating a string object like this

var txt = new String("Hello World");

and this

var txt = "Heloo World";
Rajasekar
  • 18,392
  • 34
  • 106
  • 137

9 Answers9

3

First of all let's bust some myths. String literal is not a syntactic sugar.

> var txt = new String("Hello World");
> typeof x
"string"
> var txt2 = "Hello World";
> typeof txt2
"object"

As you can see these two things are of different type. Even more: one of them is object and the second is not. This has a practical consequence, like:

> new String("Hello World") == new String("Hello World")
false
> "Hello World" == "Hello World"
true

Another difference is that primitives are passed by value while objects by reference. This might save some memory if you are passing around lots of big strings. However both objects and literals are immutable so it really isn't a big deal (why would you pass lots of big strings if you're not going to modify them?).

Also you can add attributes to objects while you can't add them to primitive types. But that's not 100% true. You can add an attribute to the prototype of a primitive. Note that both literals and objects have the same prototype:

> String.prototype.test = 11;
> var x = new String("x");
> x.test;
11
> var y = "x":
> y.test;
11
freakish
  • 54,167
  • 9
  • 132
  • 169
1

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"

Hope this helps..

Sai Avinash
  • 4,683
  • 17
  • 58
  • 96
1

The String constructor function returns an object of type String. Usually, you want a literal string value, because strictly comparing an object to a literal will always return false.

danwarfel
  • 890
  • 1
  • 6
  • 13
1

One is a string primitive and the other is a String object. The advantage of String object over string primitive is that it can store properties

var x = "foobar"
x.y = "hello"
console.log(x.hello) /* Undefinded*/

var z = new String("foobar")
z.y = "hello"
console.log(z.y) /* hello*/

typeof x = "string"
typeof z = object.

also

x==z //true
x===z // false
Konza
  • 2,143
  • 17
  • 30
1

Nothing much but beware of this,

var s = new String('A');
var s1 = String('A');
var s2 = 'A';


console.log(s === 'A');//fasle -- since type is Object
console.log(s1 === 'A');//true
console.log(s2 === 'A');//true
Sarath
  • 9,030
  • 11
  • 51
  • 84
0

The first returns a String object, the second returns a string.

typeof txt; //object
typeof txt2; //string

While they may act similar, txt !== txt2

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
  • 1
    Literal strings "inherit" from `String.prototype` as well. – freakish Feb 13 '14 at 07:46
  • that's correct, didn't even notice that – Sterling Archer Feb 13 '14 at 07:51
  • @freakish: No they don't: `"foo" instanceof String == false` – Bergi Feb 13 '14 at 08:04
  • @Bergi That's why I used quatation mark. What I've meant is that literals can use methods of `String.prototype`. Besides `"test".__proto__ == String.prototype`. – freakish Feb 13 '14 at 08:08
  • @freakish: Property access on primitives is magic and does not count. `String.prototype.isPrototypeOf("") == false`. – Bergi Feb 13 '14 at 08:12
  • @Bergi It absolutely **does** count. See how people define custom methods like `.startswith` on string literals. And it's not magic, it's just that we don't understand it (probably explained somewhere in ECMAScript specification since it is the same in all JS engines). – freakish Feb 13 '14 at 08:14
  • Yes it is explained there, and when you read it you will understand that primitive values have neither properties nor methods… – Bergi Feb 13 '14 at 08:15
0
var txt = "Heloo World";

here 'txt' is a primitive data type. It has no methods, it is nothing more than a pointer to a raw data memory reference, which explains the much faster random access speed.

var txt = new String("Hello World");

If you use new, you're explicitly stating that you want to create an instance of an Object. Therefore, new String is producing an Object wrapping the String primitive, which means any action on it involves an extra layer of work.

Talha
  • 18,898
  • 8
  • 49
  • 66
  • It's not true: literal string does have methods. Even more: it has the same prototype as `String`. Try adding any method to `String.prototype` and you will see that it is available in literal strings as well. So the only difference is that one is an object and the second is not. – freakish Feb 13 '14 at 07:42
  • @freakish: No. Primitive values do not have values. However, when you use a property assessor on a primitive value some magic happens… – Bergi Feb 13 '14 at 08:07
  • @Bergi What is the difference between "having a method" and "able to access a method" from programmers point of view? – freakish Feb 13 '14 at 08:16
  • @freakish: Probably an irrelevant one in practise. However, what is the difference between "having methods" and "able to assign a method"? – Bergi Feb 13 '14 at 08:19
  • @Bergi I'm sure you'll agree that there is a difference between "I have" and "I can't have more". `"x".test = 1` simply does not work (it should at least throw an exception IMHO, not just silently fail). – freakish Feb 13 '14 at 08:53
  • @freakish: Yeah, you're right there is. But the point of this question is that programmers should not be tricked into "*it has properties -> it is an object -> I can add and access custom properties*" by JavaScript's "Everything is an object" paradigm. – Bergi Feb 13 '14 at 08:59
0

The primitive data types are:

  • String
  • Number
  • Boolean

If you using:

var txt = "Heloo World";

this is primitive type data type alert(typeof txt); will give you string type.

In JavaScript all variable can be of object type, for each primitive their is a Object type.

var txt = new String("Heloo World");

here typeof will give you object.

For difference see jsfiddle

var str="text"; 
alert(typeof str);     //string

var str2="text";
alert(str === str2);   //true

str = new String("text changed");
alert(typeof str);     //object

var str2 = new String("text changed");
alert(str === str2);   //false
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
  • 1
    Worth noting: `undefined` and `null` are the other two primitive data types. – danwarfel Feb 13 '14 at 07:52
  • see article, these are Special Data Types. – Zaheer Ahmed Feb 13 '14 at 07:55
  • 1
    @ZaheerAhmed Not according to ECMAScript specification: http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf `A primitive value is a member of one of the following built-in types: Undefined, Null, Boolean, Number, and String` – freakish Feb 13 '14 at 08:02
  • @freakish thanks, yes they are primitive type, but usually no other language has these data type thats y I say these are special primitive data types. thanks for the links. – Zaheer Ahmed Feb 13 '14 at 08:07
  • Cool! Let's argue semantics, @ZaheerAhmed! Thank you, @freakish – you beat me to it. – danwarfel Feb 13 '14 at 08:21
-1

Nothing, really. In the first example you call the constructor of class String and put as a parameter the body of the string, where as in the second example it is done automatically at the interpretation phase.

So basically the second is syntactic sugar, where as the first isn't.

There is no difference in the functionality.

Stanimirovv
  • 3,064
  • 8
  • 32
  • 56
  • -1: there are differences in functionality and literal string **is not** a syntactic sugar (otherwise how would you pass a string to the constructor of `String`?). See other people answers. – freakish Feb 13 '14 at 07:44
  • A fair point, I should do some research on it. – Stanimirovv Feb 13 '14 at 07:50