Can you please explain the following piece of code, ? it is working in my browser console. So how does this work ? The new keyword doesnt create a new instance at all or how is it ?
var myObject = new Object(); // Produces an Object() object.
myObject['0'] = 'f';
myObject['1'] = 'o';
myObject['2'] = 'o';
console.log(myObject); // Logs Object { 0="f", 1="o", 2="o"}
var myString = new String('foo'); // Produces a String() object.
console.log(myString); // Logs foo { 0="f", 1="o", 2="o"
Please explain.
It doesn’t. You are initialising your String object with a string literal:
That foo is an entirely different foo to the characters you assign to the three properties of the object. For comparison, replace the second foo with bar.