JavaScript string initialization

后端 未结 5 564
暗喜
暗喜 2021-02-04 08:39

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

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

Which one is

相关标签:
5条回答
  • 2021-02-04 08:54

    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.

    0 讨论(0)
  • 2021-02-04 08:54

    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.

    0 讨论(0)
  • 2021-02-04 09:05

    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.

    0 讨论(0)
  • 2021-02-04 09:06

    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().

    0 讨论(0)
  • 2021-02-04 09:12

    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.

    0 讨论(0)
提交回复
热议问题