
- 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
Returning values from a constructor in JavaScript?
Following is the code for returning values from a constructor in JavaScript −
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 { font-size: 20px; font-weight: 500; } </style> </head> <body> <h1>Returning value from constructor</h1> <div style="color: green;" class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to return a object from constructor</h3> <script> let resEle = document.querySelector(".result"); function Human(){ this.firstName = 'Rohan'; this.lastName = 'Sharma'; this.age = 22; return {firstName:'Mohit',lastName:'Sharma'}; } let obj = new Human(); document.querySelector(".Btn").addEventListener("click", () => { for(i in obj){ resEle.innerHTML += 'Key = ' + i + ' : Value = ' + obj[i] + '<br>' } }); </script> </body> </html>
Output
The above code will produce the following output −
The above code will produce the following output −
- Related Articles
- Returning two values from a function in PHP
- Returning multiple values from a C++ function
- Returning array values that are not odd in JavaScript
- Returning lengthy words from a string using JavaScript
- Returning multiple values from a function using Tuple and Pair in C++
- Returning only odd number from array in JavaScript
- PHP Returning values
- Returning Multiple Values in Python?
- Returning the highest value from an array in JavaScript
- Returning the highest number from object properties value – JavaScript
- Function Returning Multiple Values in Go Language
- Returning Value from a Subroutine in Perl
- Returning the second most frequent character from a string (including spaces) - JavaScript
- Returning acronym based on a string in JavaScript
- Function returning another function in JavaScript

Advertisements