JavaScript ES6 Arrow Function Default Parameters & Multiple Return Values
In this post we will give you information about JavaScript ES6 Arrow Function Default Parameters & Multiple Return Values. Hear we will give you detail about JavaScript ES6 Arrow Function Default Parameters & Multiple Return ValuesAnd how to use it also give you demo for it if it is necessary.
Just like most programming languages, JavaScript provides functions which are logical unit of code instructions that execute a specific task or implement a certain algorithm. A function has zero or more parameters and a single return value.
How to Declare a JavaScript Arrow Function?
Let’s take an example of a JavaScript function using the arrow syntax:
constfunc=()=>{// Other instructions}
How to Declare a JavaScript Arrow Function with Parameters
This function takes no parameter. Let’s now see a function with one parameter:
constfunc=param=>{// Other instructions}
Or also two parameters:
constfunc=(param1,param2)=>{// Other instructions}
How to Declare JavaScript Functions with Default Values?
In ES6, functions can take parameters with default values:
constfunc=(param1='param1',param2='param2')=>{// Other instructions}
So now if you call the func
function without passing any parameters:
func();
The param1
and param2
variables will have the ‘param1’ and ‘param2’ values as initial values.
How to Return a Value from A JavaScript Function?
JavaScript functions always return a value. If no value is explicetly returned from the function, undefined
is returned.
You can use the return
statement to return a single value from a function. For example:
constfunc=(param1='param1',param2='param2')=>{returnparam1+' '+param2;}
We can call the function and assign the returned value to a variable like so:
constret=func();// ret = 'parma1 param2'
How to Return Multiple Values from a Function in JavaScript?
You can only return one value from a function. But you can use various way to simulate returning multiple values.
You can use arrays and objects and use object/array destructuring assignment to get individual values.
Let’s see this by example:
constfunc=(firstName='kaima',lastName='Abbes')=>{return[firstName,lastName];}const[firstName,lastName]=func();
We call the function which returns an array and we use the array destructuring syntax to get the individual return values.
Let’s see another example using an object:
constfunc=(firstName='kaima',lastName='Abbes')=>{return{firstName:firstName,lastName:lastName};}const{firstName,lastName}=func();
We call the func()
function and we use the object destructuring assignment to get individual return values.
Conclusion
In this post, we’ve seen how to use arrow functions with default parameters and multiple return values.
Hope this code and post will helped you for implement JavaScript ES6 Arrow Function Default Parameters & Multiple Return Values. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs