JavaScript string initialization

In JavaScript, in my opinion, everything is the same below:

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

Which one is most preferred?

+8
javascript
source share
5 answers

Do not use

 var str = new String(); 

Because the

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

But

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

However, due to the type of 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 quotation marks do not matter, except that quotation marks must be escaped. Many others have pointed out that single quotes are better for creating HTML strings, since XHTML expects attributes to have double quotes, and you won no need to avoid them.

+7
source share

When executing new String() , the string primitive is not returned to you, but << 22>.

For all purposes and purposes, it acts as a primitive string , but there are times when it will not. 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, a problem may arise. When you compare objects in JavaScript, this is only true if they are the same exact object, and 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 recommend using var a = '' or var a = "" . As for single quotes and 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 you need to use ' or " , but I suggest those over new String() .

+6
source share

As long as '' and "" match (they are primitives), new String() not , because it returns a String object.

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

See The Difference Between String Primitives and String Objects .

+2
source share

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

  • This is not verbal
  • Double quotes may interfere with double attribute quotes inside strings.

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

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

+1
source share

Definitely without calling the constructor function.

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

Please note that such an optimization (literal versus constructor), I believe, will only matter if you have hundreds or thousands of these statements.

-one
source share

All Articles