1

New to Javascript - how to print value of "sam" outside the employee object

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

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

Thanks in Advance!!

1
  • 3
    You have to call the details function employee.details() Commented Aug 5, 2020 at 13:28

1 Answer 1

4

You are updating the value of empname to Sam in details method , you need to call that method and then you use it .

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

Sign up to request clarification or add additional context in comments.

2 Comments

Great to hear it helped you . I hope you wont mind accepting the answer for better visibility.
You can upvote my question too. If you found it a good questions for learner(freshers)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.