admin管理员组

文章数量:1180531

In JavaScript, from my understanding, the below are all same:

var carter2 = new String();
var carter2 = '';
var carter2 = "";

Which one is the most preferred?

In JavaScript, from my understanding, the below are all same:

var carter2 = new String();
var carter2 = '';
var carter2 = "";

Which one is the most preferred?

Share Improve this question edited Dec 25, 2014 at 2:34 hexacyanide 91.5k31 gold badges165 silver badges162 bronze badges asked Mar 30, 2012 at 16:21 Nate PetNate Pet 46.2k127 gold badges274 silver badges420 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 12

Don't use

var str = new String();

Because

var str = new String("dog");
var str2 = new String("dog"); 
str == str2; // false

But

var str = "dog";
var str2 = "dog"; 
str == str2; // true

However, because of type coercion, the following works (Thanks to Rocket for pointing it out)

var str = new String("dog");
var str2 = "dog"; 
str == str2; // true

Single and double quotes don't matter, except for what quotes need to be escaped. Many others have noted that single quotes are better when creating HTML strings, since XHTML expects attributes to have double quotes, and you won't need to escape them.

When doing new String(), you are not being returned a string primitive, but a String object.

For all intents and purposes it acts like a string primitive, but there are cases where it won't. For example:

var a = new String('body');
jQuery(a); // This will *not* function as expected
           // This will make a jQuery object containing a "String" object
           // It will NOT treat it as a selector string

Also when comparing things, there may be problem. When you compare objects in JavaScript, it's only true if they are the same exact object, not just the same value. Example:

var a = new String('test');
var b = new String('test');
var c = a;
var d = 'test';

a === b; // false, a and b are different objects (a == b is also false)
c === a; // true, a is the same object as c
c === b; // false, c (which is a) is a different object than b
d === a; // false, d is a primitive and a is an object, they are not equal
'test' === d; // true, they are both string primitives

d == a; // true, this uses "==" so only value is compared, not type

You can use .valueOf() to convert a String object to a string primitive.

new String('test').valueOf() === 'test'; // true

So, I highly suggest using var a = '' or var a = "". As for single quotes vs. double quotes, there is no difference. Consider this example:

var a = "My name is Joe O'Ryan";
var b = 'My name is Joe O\'Ryan'; // need to escape the '
var c = 'Joe said: "Hi"';
var d = "Joe said \"HI\""; // need to escape the "

So, it's up to you whether to use ' or ", but I suggest those over new String().

While '' and "" are the same (they are primitives), new String() is not because it returns a String object.

typeof '' == 'string'
typeof "" == 'string'
typeof new String() == 'object'

See Distinction between string primitives and String objects.

I prefer to use var carter2 = ''; because:

  1. It's not verbosive
  2. Double quotes might clash with attributes double-quotes inside strings.

So I prefer to use single quotes in java-script, double quotes for html attributes.

UPD: this is my preference, I know that single-double quotes are interchangeable.

Definitely without the constructor function call.

So with either single or double quotes (i.e. literal).

Note, this kind of optimization (literal vs. constructor) I believe would only matter if you had hundreds or thousands of these statements though.

本文标签: JavaScript string initializationStack Overflow