0% completed
Default parameters allow functions to have pre-defined values if no arguments are provided, or if undefined
is passed. They simplify function calls and enhance the functionality of functions without the need for additional checks inside the function body.
Why We Need Default Parameters?
In earlier versions of JavaScript, handling missing arguments required additional code inside functions to assign default values. This could lead to verbose and less readable code, especially in functions with multiple parameters. Default parameters address this issue by allowing developers to set default values right in the function signature, making the function easier to use and understand.
Example: Function Without Default Parameters
Initially, handling missing arguments might look something like this:
- This example uses a logical OR (
||
) to provide a default value ('Guest') ifname
is not provided or isundefined
. - Calling
greet()
without an argument defaults to greeting a 'Guest'.
Example: Function With Default Parameters
With ES6 default parameters, the same functionality can be achieved more cleanly:
- The function
greet
now directly assigns a default value ('Guest'
) toname
in the function signature. - This makes the code cleaner and the default value assignment more explicit.
Default Parameters with Multiple Arguments
Default parameters are especially useful in functions with multiple parameters, enhancing readability and reducing the amount of code needed for default value assignments.
Example: Multiple Default Parameters
Here, we define a function that requires multiple parameters, each with its own default value.
orderPizza
has two parameters, each with a default value:type
defaults to'Cheese'
, andsize
defaults to'Medium'
.- Calling
orderPizza()
without arguments uses both default values. - Calling
orderPizza('Pepperoni')
uses the default value forsize
but overrides the default value fortype
.
Passing Expressions as Default Parameters
Default parameters in JavaScript functions are not limited to static values
; they can also be expressions. When an expression is used as a default parameter, it's evaluated at function call time if the parameter is omitted or undefined
. This feature allows for dynamic initialization of function parameters based on runtime conditions or other function calls.
Example: Dynamic Default Parameter with Expression
In this example, we'll create a function that logs a message with a timestamp, using an expression as the default parameter to generate the timestamp dynamically.
function logMessage(message, timestamp = new Date().toLocaleTimeString()) { ... }
defines a functionlogMessage
with two parameters:message
andtimestamp
.message
is a required parameter for the message to log.timestamp
has a default value that is an expression. Iftimestamp
is not provided when the function is called, JavaScript will executenew Date().toLocaleTimeString()
to generate a default timestamp.
new Date().toLocaleTimeString()
generates a string representing the current time according to the locale of the environment running the script. This expression is evaluated each time the function is called without atimestamp
argument, ensuring the timestamp reflects the call time.- When
logMessage("Hello, world!");
is called without atimestamp
, the function uses the current time as the default value fortimestamp
and logs the message with this dynamically generated timestamp.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible