Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.



Get Started Now!

How do I use a default parameter in JavaScript?

JavaScript

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).

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x