How a Function Returns an Object in JavaScript ?
Last Updated :
06 Oct, 2023
JavaScript Functions are versatile constructs that can return various values, including objects. Returning objects from functions is a common practice, especially when you want to encapsulate data and behavior into a single entity. In this article, we will see how a function returns an object in JavaScript.
Why return objects from Function?
- Encapsulation: Functions bundle related data and behavior, making code more organized.
- Reusability: Objects returned by functions can be used across the application.
- Data Hiding: You can control access to data within an object.
How a Function Returns an Object?
When a function returns an object in JavaScript, it follows a specific sequence of steps. Let's break down the working of a function returning an object
Function Definition
The process starts with defining a JavaScript function. This function will be responsible for creating and returning an object. The function can have parameters that allow you to customize the properties and values of the object it will return. Here's an example of a function declaration:
function createPerson(name, age) {
// Function logic goes here
}
Object Creation
Within the function, you create a new object. You can initialize this object in various ways, either by using object literal notation or by calling a constructor function. The object will typically have properties and values, which can be set based on input parameters or any other logic you need:
function createPerson(name, age) {
let person = {
name: name,
age: age,
};
}
In this example, we create an object "person" with "name" and "age" properties based on the "name" and "age" parameters passed to the function.
Return Statement
To return the newly created object from the function, you use the return statement. The return statement specifies the value (in this case, the object) that the function will provide as its result when called:
function createPerson(name, age) {
let person = {
name: name,
age: age,
};
return person;
}
Function Invocation
To obtain the object returned by the function, you call the function and assign the returned value to a variable:
let john = createPerson("John", 30);
In this line of code, we call the "createPerson" function with arguments "John" and "30", and the returned object is assigned to the variable "john".
Using the Object
Now, you can work with the john object just like any other JavaScript object. You can access its properties, modify them, or use the object in your code as needed:
console.log(john.name); // Output: "John"
console.log(john.age); // Output: 30
Complete Implementation
Example: This example implements the above steps combined in sequence.
JavaScript
function createPerson(name, age) {
let person = {
name: name,
age: age,
};
return person;
}
let john = createPerson("John", 30);
console.log(john.name); // Output: "John"
console.log(john.age); // Output: 30
Similar Reads
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. Tabl
2 min read
How to write a function in JavaScript ?
JavaScript functions serve as reusable blocks of code that can be called from anywhere within your application. They eliminate the need to repeat the same code, promoting code reusability and modularity. By breaking down a large program into smaller, manageable functions, programmers can enhance cod
4 min read
Are functions objects in javascript?
Yes, Functions are considered first-class objects, which means they have the same capabilities as other objects. The following are examples that demonstrates functions behaving as objects: Can be assigned to variable [GFGTABS] JavaScript // Assign a function to a variable const x = function() { retu
1 min read
How to Store a JavaScript Fnction in JSON?
In JavaScript, we can store JavaScript functions in JSON objects, allowing us to serialize and store executable code alongside data. we will explore three approaches to store a JavaScript function in JSON. These are the following approaches: Table of Content Using JSON.stringify()Using a Custom toJS
2 min read
JavaScript SyntaxError - Return not in function
This JavaScript exception return (or yield) not in function occurs if a return/yield statement is written outside the body of the function. Message: SyntaxError: 'return' statement outside of function (Edge)SyntaxError: return not in function (Firefox)SyntaxError: yield not in function (Firefox)Erro
2 min read
How to print the content of an object in JavaScript ?
To print the content of an object in JavaScript we will use JavaScript methods like stringify, object.values and loops to display the required data. Let's first create a JavaScript Object containing some key-values as given below: C/C++ Code // Given Object const obj = { name: 'John', age: 30, city:
3 min read
How to transform String into a function in JavaScript ?
In this article, we will see how to transform a String into a function in JavaScript. There are two ways to transform a String into a function in JavaScript. The first and easy one is eval() but it is not a secure method as it can run inside your application without permission hence more prone to th
3 min read
How to Pass Object as Parameter in JavaScript ?
We'll see how to Pass an object as a Parameter in JavaScript. We can pass objects as parameters to functions just like we do with any other data type. passing objects as parameters to functions allows us to manipulate their properties within the function. These are the following approaches: Table of
3 min read
How to pass primitive/object types through functions in JavaScript ?
In this article, we learn how JavaScript primitive/object types passed through functions. First, we will see, what are primitive and object data types in JavaScript: 1. Primitive data type: The predefined data types that are provided by JavaScript are called primitive data type. The primitive data t
4 min read
How to check whether an object exists in javascript ?
Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value. This helps avoid errors when accessing or manipulating objects that may be undefined, null, or not initialized properly. Here we have some common approaches t
3 min read