Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

Solution 2:

How about:

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

Your other option is:

this.hasWheels = arguments.length > 0 ? hasWheels : true;

Solution 3:

There are variations to be noted of from posted answers.

varVar = function( value ) {
    this.value0 = value !== false;
    this.value1 = value !== false && value !== 'false';
    this.value2 = arguments.length <= 0 ? true : arguments[0];
    this.value3 = arguments[0] === undefined ? true : arguments[0];
    this.value4 = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
};

                     value0   value1   value2        value3         value4
---------------------------------------------------------------------------
Var("")              truetruetruetruetrueVar("''")            truetrue''''''Var("0")             truetrue000Var("'0'")           truetrue'0''0''0'Var("NaN")           truetrueNaN           NaN            NaN
Var("'NaN'")         truetrue'NaN''NaN''NaN'Var("null")          truetruenullnullnullVar("'null'")        truetrue'null''null''null'Var("undefined")     truetrueundefinedtruetrueVar("'undefined'")   truetrue'undefined''undefined''undefined'Var("true")          truetruetruetruetrueVar("'true'")        truetrue'true''true''true'Var("false")         falsefalsefalsefalsefalseVar("'false'")       truefalse'false''false''false'
  • value1 is made especially from value0 for string 'false' if one needs it to be boolean false. I found this relaxation useful occationally.
  • value2 and value3 are modifications of original posted answers for consistency, without changed results.
  • value4 is how Babel compiles for default parameters.

Solution 4:

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 parametervarCar = function(hasWheels = true) {
  this.hasWheels = hasWheels;
}

var myCar = newCar();
console.log(myCar.hasWheels); // truevar myOtherCar = newCar(false)
console.log(myOtherCar.hasWheels); // false

Solution 5:

Without much confusion you can do like this to get a default true.

this.hasWheels=typeof hasWheels === 'boolean'?hasWheels:true

To get a default false

this.hasWheels=typeof hasWheels === 'boolean'?false

Post a Comment for "How To Set Default Boolean Values In Javascript?"