Default Parameter
Syntax: –
function function_name (para1, para2=“value”, para3) // problem undefined
{
Block of statement;
}
Syntax: –
function function_name (para1, para2=“value1”, para3=“value2”)
{
Block of statement;
}
JavaScript also allows the use of arrays and null as default values.
Rest Parameters
The rest parameter allows representing an indefinite number of arguments as an array.
Syntax: –
function function_name (…args)
{
Block of statement;
}
Syntax: –
function function_name (a, …args)
{
Block of statement;
}
The rest operator must be the last parameter to a function.
Rest Vs Arguments
There are three main differences between the rest parameters and the arguments object:-
- Rest parameters are only the ones that haven’t been given a separate name, while the arguments object contains all arguments passed to the function.
- The arguments object is not a real array, while Rest Parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly.
- The arguments object has additional functionality specific to itself (like the callee property).
- Astropilot: Elevating Pilot Training to New Heights - December 28, 2024
- Discover Ranchi with Motoshare’s Bike and Car Rental Services - December 16, 2024
- Explore Mumbai with Motoshare’s Easy Bike and Car Rentals - December 16, 2024