In this article we are going to understand various return types of JavaScript function.
Return type of JavaScript function
In this article we will learn about various return types of JavaScript
function. We know that JavaScript is object oriented programming language and
function in JavaScript can able to return various type of value. Now, we will
understand them one by one.
Actually JavaScript is loosely type programming language and
we can return any data type from JavaScript function.
Return Boolean type.
We can return any predefined data type like Boolean, string,
array and many more. Here hello() function is returning Boolean value. Here is
sample code.
<script>
function hello() {
return true;
}
alert(hello());
</script>
Output is here.

Return object from function
We can return object from JavaScript function. In this example
we are returning “person” object form hello() function and after that we are
showing the return value. Here is sample example.
<script>
function hello() {
var person = new Object();
person.name = "Sourav";
person.surname = "Kayal";
return person;
}
var p = hello();
alert(p.name + " " + p.surname);
</script>
Output is here.

Function returns function
This technique of JavaScript is called “closure”. In
JavaScript one function can return another function. In this example
the fun1 function is returning the innerfunction() and after that we are
showing the return value of innerfunction() within alert message.
<form id="form1" runat="server">
<script>
var fun1 = function () {
return function innerfun() {
return "Function return";
}
}
var v = fun1();
alert(v());
</script>
</form>
This is the example of closure in JavaScript. Here is sample
output.

Return JSON data
We can return JSON data from function and then we can parse it. In
this example fun() function is returning JSON data. The json data contains two
key value pair called name and surname.
<form id="form1" runat="server">
<script>
function fun() {
return '{"name":"sourav","surname":"kayal"}';
}
var value =JSON.parse(fun());
alert(value.name + value.surname);
</script>
</form>
We are using JSON.parse() method to parse JSON data. Here is
sample output of this application.
Conclusion:-
In this article we have learned various return type of JavaScript function. Hope you have enjoyed this article.