Amie Ambriz
Amie Ambriz

Reputation: 7

How to set default value for Function object?

Is there a better way to add a default value to a Function object?

 function CountOfFruit(bowl){
    this.strawberries = bowl.strawberries
    this.blueberries = bowl.blueberries
    
   if (bowl.strawberries == ""){
     this.strawberries = "there is none"
   }
 }

Upvotes: 0

Views: 112

Answers (1)

Yes, you can write something like:

function CountOfFruit(bowl){
    this.strawberries = bowl.strawberries || "there is none";
    this.blueberries = bowl.blueberries || "there is none";
}

In that way, if one of the attributes is null or an empty string, the default value will be used.

Upvotes: 2

Related Questions