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

前端 未结 12 1665
孤街浪徒
孤街浪徒 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:42

    we can define String in 3-ways

    1. var a = "first way";
    2. var b = String("second way");
    3. var c = new String("third way");

    // also we can create using 4. var d = a + '';

    Check the type of the strings created using typeof operator

    • typeof a // "string"
    • typeof b // "string"
    • typeof c // "object"


    when you compare a and b var a==b ( // yes)


    when you compare String object

    var StringObj = new String("third way")
    var StringObj2 = new String("third way")
    StringObj  == StringObj2 // no result will be false, because they have different references
    

提交回复
热议问题