Tip 28 Create Default Parameters

In this tip, you’ll learn how to use default parameters to set values when a parameter is absent.

No matter how much planning you do, things change. This is especially true of function parameters. When you write a function, you expect a few parameters. Then, as code grows and edge cases emerge, suddenly the parameters you supplied aren’t enough.

In the next several tips, you’ll learn different techniques for handling parameters. Nearly all of these techniques can help you cope in some way with changing requirements. But to start, you’ll learn the easiest trick: setting default parameters.

Consider a basic helper function. All you want to do is convert pounds to kilograms. That seems simple. You simply need to ...

Get Simplifying JavaScript now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.