JS Daily #20 - Return From Constructor Function

What is the output of following code?

function Car(name) {
  this.name = name;
  return "BMW";
}

const carObj = new Car("Mercedes");
console.log(carObj.name);
----o----

The output is "Mercedes".

Here the constructor function Car() is explicitly returning a string value. But that is ignored when used with new keyword. The newly created instance object is assigned to carObj. Therefore carObj.name outputs "Mercedes".

If the returned value is an object instead of "BMW", then that object is assigned to carObj. The newly created object is ignored.