Defining Default Values for Parameters

Parameters can take default values that step in for any missing arguments. We can benefit from default parameters in one of three ways:

  • As a user of a function, we don’t have to pass in a value for a parameter if the value we intend to send is the same as the sensible default chosen by the creator of the function—resulting in less work and reduced noise/clutter in code.

  • As the author of a function, we can evolve the function signature more freely, to add a new parameter, without breaking existing code.

  • We can compensate for the lack of function overloading in JavaScript. Many modern languages provide function overloading, but JavaScript does not. With default parameters, the caller may pass a different ...

Get Rediscovering 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.