How to set default boolean values in JavaScript?

后端 未结 5 1524
执念已碎
执念已碎 2021-01-31 01:38

Setting default optional values in JavaScript is usually done via the || character

var Car = function(color) {
  this.color = color || \'blue\';
};
         


        
5条回答
  •  孤街浪徒
    2021-01-31 01:59

    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

提交回复
热议问题