Setting default optional values in JavaScript is usually done via the ||
character
var Car = function(color) {
this.color = color || \'blue\';
};
You can use the Default function parameters feature in ECMA6. Today, ECMA6 is still not fully supported in the browser but you can use babel and start using the new features right away.
So, the original example will become as simple as:
// specify default value for the hasWheels parameter
var Car = function(hasWheels = true) {
this.hasWheels = hasWheels;
}
var myCar = new Car();
console.log(myCar.hasWheels); // true
var myOtherCar = new Car(false)
console.log(myOtherCar.hasWheels); // false