
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Is there an elegant way of passing an object of parameters into a function?
Following is the code to pass an object of parameters to a function −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result,.sample { font-size: 18px; font-weight: 500; color: rebeccapurple; } .sample { color: red; } </style> </head> <body> <h1>Passing an object of parameters into a function</h1> <div class="result"><br /></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to add 4 object values by passing them to a function</h3> <script> let resEle = document.querySelector(".result"); let BtnEle = document.querySelector(".Btn"); function addNum({ a, b, c, d }) { return a + b + c + d; } let obj = { a: 22, b: 33, c: 7, d: 9, }; BtnEle.addEventListener("click", () => { resEle.innerHTML = " The sum of the parameters = " + addNum(obj); }); </script> </body> </html>
Output
The above code will produce the following output −
On clicking the ‘CLICK HERE’ button −
- Related Articles
- Is there a way to print all methods of an object in JavaScript?
- Is there any way to embed a PDF file into an HTML5 page?
- Is there any way to check if there is a null value in an object or array in JavaScript?
- Passing an array to a C++ function
- Convert an array of datetimes into an array of strings passing units in Python
- Is there an easy way to rename a table in a MySQL procedure?
- Flat a JavaScript array of objects into an object
- Convert an array of datetimes into an array of strings passing hour datetime unit in Python
- Convert an array of datetimes into an array of strings passing minutes datetime unit in Python
- Splitting an object into an array of objects in JavaScript
- Best way to flatten an object with array properties into one array JavaScript
- The most elegant way to iterate the words of a C/C++ string
- The most elegant way to iterate the words of a string using C++
- How to get an object containing parameters of current URL in JavaScript?
- Is there a way to name columns in an INSERT statement in MySQL?

Advertisements