How to return an object from a JavaScript function?

JavascriptWeb DevelopmentFront End Technology

To return an object from a JavaScript function, use the return statement, with this keyword.

Example

You can try to run the following code to return an object from a JavaScipt function −

 Live Demo

<html>
   <head>
      <script>
         var employee = {
            empname: "David",
            department : "Finance",
            id : 002,
            details : function() {
               return this.empname + " with Department " + this.department;
            }
         };

         document.write(employee.details());
      </script>
   </head>
</html>

Output

raja
Published on 23-Feb-2018 11:58:40
Advertisements