Handling Default Values - jpjohnsonjr/learning-notes GitHub Wiki

Common coding scenario

Consider the following code:

 2 function orderChickenWith(sideDish) {
 3   console.log(Chicken with " + sideDish);
 4 }
 5
 6 orderChickenWith("noodles");
 7 orderChickenWith();
 8

First execution of the function displays "Chicken with noodles". Second displays "Chicken with undefined". solution is to place a default value that will display in the event that no value has been defined. For example:

if (sideDish === undefined) { 
  sideDish = "whatever!";
}

This default value is verbose. A shortcut would be sideDish = sideDish || "whatever!";.