GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||
GeezerVison is showing www.tutorialspoint.com — How to pass an object as a parameter in JavaScript function? ![]() Article Categories
Selected Reading
How to pass an object as a parameter in JavaScript function?In this tutorial, we will learn how to pass an object as a parameter in a JavaScript function. One of JavaScript's data types is represented by the Object type. It is used to store keyed collections as well as more complex entities. The Almost all JavaScript objects are instances of Object; a typical object inherits properties (including methods) from Following are the methods used to pass an object as a parameter in a JavaScript function. Using Object Literal NotationThis is the most straightforward approach. You place your key-value pairs separated by ':' inside a set of curly braces, and your object is ready to use. We can pass an object to a JavaScript function using destructuring, where the function parameters must have the same names as the object property names. Syntaxfunction functionName({ property1, property2 }) {
// function body using property1 and property2
}
let objectName = {
property1: value1,
property2: value2
};
functionName(objectName);
ExampleThe <html>
<body>
<p id="result"></p>
<script>
let output = document.getElementById("result");
// define a function with destructuring
function areaOfRectangle({l, b}) {
return (l * b);
}
// define an object
let rectangle = {l: 77, b: 21 };
// Call the function passing the object as parameter
let area = areaOfRectangle(rectangle);
output.innerHTML = "Area of rectangle: " + area + "<br>";
</script>
</body>
</html>
Area of rectangle: 1617 Passing Entire Object as ParameterYou can also pass the entire object to a function and access its properties using dot notation inside the function. Example<html>
<body>
<p id="result2"></p>
<script>
let output = document.getElementById("result2");
// Function that accepts entire object
function calculateArea(shape) {
return shape.length * shape.width;
}
// Create object
let rectangle = {
length: 25,
width: 15,
color: "blue"
};
// Pass entire object
let area = calculateArea(rectangle);
output.innerHTML = "Rectangle area: " + area + "<br>" +
"Rectangle color: " + rectangle.color;
</script>
</body>
</html>
Rectangle area: 375 Rectangle color: blue Using Constructor Functions and PrototypesJavaScript allows creating objects using constructor functions. The Syntaxfunction ConstructorName(param1, param2) {
this.param1 = param1;
this.param2 = param2;
}
ConstructorName.prototype.methodName = function() {
return this.param1;
};
ExampleIn this example, we create a constructor function and use prototypes to add methods. The object is created using the <html>
<body>
<p id="result3"></p>
<script>
let output = document.getElementById("result3");
// Constructor function
function Student(name, grade) {
this.name = name;
this.grade = grade;
}
// Add method using prototype
Student.prototype.getInfo = function() {
return "Student: " + this.name + ", Grade: " + this.grade;
};
// Function that accepts object parameter
function displayStudent(student) {
return student.getInfo();
}
// Create object using constructor
let student1 = new Student("Alice", "A+");
// Pass object to function
let info = displayStudent(student1);
output.innerHTML = info;
</script>
</body>
</html>
Student: Alice, Grade: A+ Comparison of Methods
ConclusionJavaScript offers multiple ways to pass objects as function parameters. Use destructuring for specific properties, pass entire objects for flexibility, or use constructor functions for object-oriented approaches. Choose the method that best fits your use case. Updated on: 2026-03-15T23:18:59+05:30
34K+ Views Learn More in Our TutorialsAdvertisements
| ||||||||||||
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |