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

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

    In case of string literal we cannot assign properties

    var x = "hello" ;
    x.y = "world";
    console.log(x.y); // this will print undefined
    

    Whereas in case of String Object we can assign properties

    var x = new String("hello");
    x.y = "world";
    console.log(x.y); // this will print world
    

提交回复
热议问题