How to Return an Object From a Function in JavaScript
- Return an Object From a Regular Function in JavaScript
- Return an Object From an Anonymous Function in JavaScript
- Return an Object From an Arrow Function in JavaScript
Whenever we say we want to return an object from a function, we return one object from another object (function). There are various ways to return an object using a function.
Let us now see how we can return an object from a function in JavaScript.
Return an Object From a Regular Function in JavaScript
There are various types of functions in JavaScript. Each of these functions is defined differently.
We will see each of these function types, and we will also see if we have to return an object using each of these functions in JavaScript and to return anything from a function, we always use a keyword known as return
.
The regular function is a traditional way of defining a function in JavaScript. This type of function is present in the JavaScript programming language from its initial versions.
This function has three things, a function
keyword, function name, and function body.
We have created a function called returnObj
to return an object. This function aims to return an object.
We have created an obj
object with two fields: the name
and the company
inside this function.
function returnObj() {
var obj = {
'name': 'Adam',
'company': 'Google',
} return obj;
}
var myObj = returnObj();
console.log(myObj);
Output:
{name: 'Adam', company: 'Google'}
We must use a return
keyword to return this object and return the obj
created.
We must create a variable myObj
and then call the returnObj
function. After we call this function, whatever the function will return (in this case, an object) will be stored inside the myObj
variable.
Finally, if you print the variable myObj
, you will get the entire object as the output.
Return an Object From an Anonymous Function in JavaScript
An anonymous function is a function that does not have a name associated with it. This type of function only has a function
keyword and a function body.
It was introduced in the ES6 version of JavaScript. Since this function does not have a name, we can’t call this function.
So, to call this function, we store this entire function into a variable first, and then we use this variable name as a function name to call this function, i.e., we add round brackets at the end.
Here, we have created a function that does not have a name. And we have created the same object obj
which we have created earlier.
const myObj =
function() {
var obj = {
'name': 'Adam',
'company': 'Google',
} return obj;
}
console.log(myObj());
Output:
{name: 'Adam', company: 'Google'}
Finally, we will return this obj
, and then after we call this function, the object we are returning from the function will be stored inside that variable.
Return an Object From an Arrow Function in JavaScript
The arrow function is also known as an anonymous function. The only difference between an arrow function and an anonymous function is that the arrow function does not use the function
keyword while declaring a function.
Instead, it uses arrows (combination of equals and greater than sign) =>
to declare a function. This type of function was also introduced in the ES6 version of JavaScript.
Here, we have created an empty object, obj
. We will create an arrow function that takes an object as a parameter (entireObj
).
Inside the arrow function’s body, we will set the name
and company
properties of the entireObj
object with some values. Then we will return the entireObj
using the return
keyword.
var obj = {};
const myObj =
(entireObj) => {
entireObj.name = 'Adam';
entireObj.company = 'Google';
return entireObj;
}
console.log(myObj(obj));
Output:
{name: 'Adam', company: 'Google'}
In the end, we will call the function myObj
. Don’t forget to pass the empty object we created at the start as the parameter while calling the myObj
function.
The result of this operation, i.e., the object, will be stored inside the myObj
variable and then printed on the console.
Sahil is a full-stack developer who loves to build software. He likes to share his knowledge by writing technical articles and helping clients by working with them as freelance software engineer and technical writer on Upwork.
LinkedIn