What is the difference between string primitives and String objects in JavaScript?

前端 未结 12 1661
孤街浪徒
孤街浪徒 2020-11-22 07:58

Taken from MDN

String literals (denoted by double or single quotes) and strings returned from String calls in a non-constructor context (i.e., witho

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 08:29

    String Literal:

    String literals are immutable, which means, once they are created, their state can't be changed, which also makes them thread safe.

    var a = 's';
    var b = 's';
    

    a==b result will be 'true' both string refer's same object.

    String Object:

    Here, two different objects are created, and they have different references:

    var a = new String("s");
    var b = new String("s");
    

    a==b result will be false, because they have different references.

提交回复
热议问题