I rarely ever use the constructed object form, e.g., (Number, String, Boolean, etc.) to create an object of the relevant type when a literal does the job. On the rare occasions when I do create, say a String object using const str = new String("something");
, I use the new
keyword. By doing this, an object is created that becomes the this
context and an instance is created such that:
const str1 = new String("hi");
const str2 = new String("hi");
console.log(str1 === str2); // false
On the contrary, if the constructed object form is used without the new
keyword,
const str1 = String("hi");
const str2 = String("hi");
console.log(str1 === str2); // true
What are the implications of this? When would you use one form over the other? Is the second way just bad practice or is there more to it than that?