How to Pass Object as Parameter in JavaScript ?
Last Updated :
22 Feb, 2024
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:
Approach 1: Passing the Entire Object In a function
In this approach, we simply pass the entire object as a parameter to a function. This allows the function to access all properties of the object without explicitly passing each property individually.
Syntax:
// Define a function that takes an object as a parameter.......
function Function_Name(objectParameter) {
// write a logic ......
}
Example: This example shows the passing of an object as a parameter to the function.
JavaScript
// Define an object
let person = {
name: "John",
age: 30
};
// Function that takes an object as a parameter
function greet(personObj) {
console.log("Hello, " + personObj.name +
"! You are " + personObj.age + " years old.");
}
// Call the function and pass
// the object as a parameter
greet(person);
OutputHello, John! You are 30 years old.
Approach 2: Destructuring the Object
When passing an object as a parameter to a function, destructuring enables us to extract specific properties directly within the function's parameter list. This approach is particularly beneficial for enhancing code readability. Using this we can do a clean code.
Example: This example shows the destructuring the object properties by parameter.
JavaScript
// Define an object
let person = {
name: "Geeks",
age: 25
};
// Function that takes destructured
// object parameters
function greet({ name, age }) {
console.log("Hello, " + name +
"! You are " + age + " years old.");
}
// Call the function and pass
// the object as a parameter
greet(person);
OutputHello, Geeks! You are 25 years old.
Approach 3: Using 'this' keyword
In JavaScript, the 'this' keyword refers to the current object . This approach is particularly useful when we define a methods within object constructors or object literals.
Example: This example shows the use of "this" keyword for accessing the object properties.
JavaScript
// Define an object constructor
function Person(name, age) {
this.name = name;
this.age = age;
// Method that uses 'this' to
// access object properties
this.greet = function () {
console.log("Hello, " + this.name +
"! You are " + this.age + " years old.");
};
}
// Create an instance of the Person object
let person = new Person("Geeks", 25);
// Call the method using 'this'
person.greet();
OutputHello, Geeks! You are 25 years old.
Similar Reads
Convert URL parameters to a JavaScript Object Given an URL with parameters, The task is to get those parameters and convert them to a JavaScript Object using javascript. we're going to discuss a few techniques. Below is the following method to convert URL parameters to a JavaScript Object: Using replace() MethodUsing split() MethodUsing for ...
2 min read
How to create object properties in JavaScript ? JavaScript is built on an object-oriented framework. An object is a collection of properties, where each property links a key to a value. These properties are not in any specific order. The value of a JavaScript property can be a method (function). Object properties can be updated, modified, added,
4 min read
How to read properties of an Object in JavaScript ? Objects in JavaScript, it is the most important data type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data-types(Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data-types all store a singl
2 min read
How to pass image as a parameter in JavaScript function ? We all are familiar with functions and their parameters and we often use strings, integers, objects, and arrays as a parameter in JavaScript functions but now will see how to pass an image as a parameter in the JavaScript functions. We will use vanilla JavaScript here. Approach: First, create a func
2 min read
How to pass an array as a function parameter in JavaScript? Here are different ways to pass an array as a function paramter in JavaScript.1. Using apply() MethodThe apply() method invokes a function with a specified `this` value and an array of arguments. It takes two parameters: the first is the `this` context for the function, and the second is an array of
2 min read