Skip to main content

Strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings.

Strings created with new String() (constructor mode) is an object and can store property in them.

Demonstrating the difference:

var strPrimitive = String('word');
strPrimitive.prop = "bar""bar";
console.log(strPrimitive.fooprop); // undefined

var strObject = new String('word');
strObject.prop = "bar""bar";
console.log(strObject.prop); // bar

Strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings.

Strings created with new String() (constructor mode) is an object and can store property in them.

Demonstrating the difference:

var strPrimitive = String('word');
strPrimitive.prop = "bar"
console.log(strPrimitive.foo); // undefined

var strObject = new String('word');
strObject.prop = "bar"
console.log(strObject.prop); // bar

Strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings.

Strings created with new String() (constructor mode) is an object and can store property in them.

Demonstrating the difference:

var strPrimitive = String('word');
strPrimitive.prop = "bar";
console.log(strPrimitive.prop); // undefined

var strObject = new String('word');
strObject.prop = "bar";
console.log(strObject.prop); // bar

added 394 characters in body
Source Link
Mamun
  • 69k
  • 9
  • 51
  • 62

Strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings.

Strings created with new String() (constructor mode) is an object and can store property in them.

Demonstrating the difference:

var strPrimitive = String('word');
strPrimitive.prop = "bar"
console.log(strPrimitive.foo); // undefined

var strObject = new String('word');
strObject.prop = "bar"
console.log(strObject.prop); // bar

Strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings.

Strings created with new String() (constructor mode) is an object and can store property in them.

Strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings.

Strings created with new String() (constructor mode) is an object and can store property in them.

Demonstrating the difference:

var strPrimitive = String('word');
strPrimitive.prop = "bar"
console.log(strPrimitive.foo); // undefined

var strObject = new String('word');
strObject.prop = "bar"
console.log(strObject.prop); // bar

Source Link
Mamun
  • 69k
  • 9
  • 51
  • 62

Strings returned from String calls in a non-constructor context (i.e., without using the new keyword) are primitive strings.

Strings created with new String() (constructor mode) is an object and can store property in them.