Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default boolean values in JavaScript?

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!

like image 937
zemirco Avatar asked Mar 17 '13 17:03

zemirco


People also ask

What is the default boolean value in JavaScript?

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 .

How do you set a boolean to default?

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.

How do you initialize a boolean value in JavaScript?

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.

How do you change boolean in JavaScript?

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.

How do you get a boolean value in JavaScript?

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.

How to set default values in JavaScript?

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.

How do you know if a boolean is false?

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.

How do you convert a parameter to a Boolean?

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 .


2 Answers

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.)

like image 122
Ted Hopp Avatar answered Sep 23 '22 23:09

Ted Hopp


How about:

this.hasWheels = (typeof hasWheels !== 'undefined') ? hasWheels : true; 

Your other option is:

this.hasWheels = arguments.length > 0 ? hasWheels : true; 
like image 27
Chris Cooper Avatar answered Sep 25 '22 23:09

Chris Cooper