Storing in an array
Insert the returned object into an array, enabling management of multiple objects in a single data structure, suitable for scenarios requiring collection-based operations.
Example: The below code uses an JavaScript array to store an object sent by a function in JavaScript.
Javascript
function returnObject() { const GFG = { name: "w3wiki" , est: 2009 } return GFG; } let objArray = []; objArray.push(returnObject()); console.log(objArray[0].name, "was established in year:" , objArray[0].est); |
Output
w3wiki was established in year: 2009
How to Store an Object sent by a Function in JavaScript ?
When a function in JavaScript returns an object, there are several ways to store this returned object for later use. These objects can be stored in variables, array elements, or properties of other objects. Essentially, any data structure capable of holding values can store the returned object.
Table of Content
- Storing in a variable
- Storing in an array