Skip to content

Commit 8cd16f0

Browse files
authored
Add example of constructor without new for String() (mdn#19864)
* Add example of constructor without new * Less trivial examples
1 parent 9fd465c commit 8cd16f0

File tree

1 file changed

+6
-2
lines changed
  • files/en-us/web/javascript/reference/global_objects/string

1 file changed

+6
-2
lines changed

files/en-us/web/javascript/reference/global_objects/string/index.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,14 @@ will automatically wrap the string primitive and call the method or perform the
113113
lookup on the wrapper object instead.
114114

115115
```js
116-
const strPrim = 'foo';
117-
const strObj = new String(strPrim);
116+
const strPrim = "foo"; // A literal is a string primitive
117+
const strPrim2 = String(1); // Coerced into the string primitive "1"
118+
const strPrim3 = String(true); // Coerced into the string primitive "true"
119+
const strObj = new String(strPrim); // String with new returns a string wrapper object.
118120

119121
console.log(typeof strPrim); // Logs "string"
122+
console.log(typeof strPrim2); // Logs "string"
123+
console.log(typeof strPrim3); // Logs "string"
120124
console.log(typeof strObj); // Logs "object"
121125
```
122126

0 commit comments

Comments
 (0)