Setting default optional values in JavaScript is usually done via the ||
character
var Car = function(color) { this.color = color || 'blue'; }; var myCar = new Car(); console.log(myCar.color); // 'blue' var myOtherCar = new Car('yellow'); console.log(myOtherCar.color); // 'yellow'
That works because color
is undefined
and undefined || String
is always the String
. Of course that also works the other way around String || undefined
is String
. When two Strings
are present the first one wins 'this' || 'that'
is 'this'
. It does NOT work the other way around as 'that' || 'this'
is 'that'
.
The question is: How can I achieve the same with boolean values?
Take the following example
var Car = function(hasWheels) { this.hasWheels = hasWheels || true; } var myCar = new Car(); console.log(myCar.hasWheels); // true var myOtherCar = new Car(false) console.log(myOtherCar.hasWheels); // ALSO true !!!!!!
For myCar
it works because undefined || true
is true
but as you can see it does NOT work for myOtherCar
because false || true
is true
. Changing the order doesn't help as true || false
is still true
.
Therefore, am I missing something here or is the following the only way to set the default value?
this.hasWheels = (hasWheels === false) ? false: true
Cheers!
If you specify any object, including a Boolean object whose value is false , as the initial value of a Boolean object, the new Boolean object has a value of true .
Set a default valueRight-click the control that you want to change, and then click Properties or press F4. Click the All tab in the property sheet, locate the Default Value property, and then enter your default value. Press CTRL+S to save your changes.
JavaScript provides the Boolean() function that converts other types to a boolean type. The value specified as the first parameter will be converted to a boolean value. The Boolean() will return true for any non-empty, non-zero, object, or array.
To toggle a boolean, use the strict inequality (! ==) operator to compare the boolean to true , e.g. bool !== true . The comparison will return false if the boolean value is equal to true and vice versa, effectively toggling the boolean.
JavaScript provides the Boolean () function that converts other types to a boolean type. The value specified as the first parameter will be converted to a boolean value. The Boolean () will return true for any non-empty, non-zero, object, or array.
Here are the 6 different ways to set the default values in javascript. The Nullish Coalescing Operator (??) 1. The OR (||) Operator In this example, we used the OR (||) operator to set default values in javascript.
If the first parameter is 0, -0, null, false, NaN, undefined, '' (empty string), or no parameter passed then the Boolean () function returns false . The new operator with the Boolean () function returns a Boolean object. Any boolean object, when passed in a conditional statement, will evaluate to true.
The value specified as the first parameter will be converted to a boolean value. The Boolean () will return true for any non-empty, non-zero, object, or array. If the first parameter is 0, -0, null, false, NaN, undefined, '' (empty string), or no parameter passed then the Boolean () function returns false .
You can do this:
this.hasWheels = hasWheels !== false;
That gets you a true
value except when hasWheels
is explicitly false
. (Other falsy values, including null
and undefined
, will result in true
, which I think is what you want.)
How about:
this.hasWheels = (typeof hasWheels !== 'undefined') ? hasWheels : true;
Your other option is:
this.hasWheels = arguments.length > 0 ? hasWheels : true;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With