0

When executing this function in the browser I get a correct and expected message

function displayInformation() {
  console.log("student with " + this.rollNo + " is " + this.name + " who is in class: " + this.class + " and he is of " + this.subject + " stream ")
};

var student1 = {
  rollNo: 100,
  name: "Rajiv",
  class: 10,
  subject: "PCMC",
  display: displayInformation
};

var student2 = {
  rollNo: 101,
  name: "Sam",
  class: 11,
  subject: "PCMB",
  display: displayInformation
};

var student3 = {
  rollNo: 102,
  name: "Ghanshyam",
  class: 12,
  subject: "commerce",
  display: displayInformation
};

student1.display();
student2.display();
student3.display();

this.name = "Raju";
this.age = 28;
this.rollNo = 103;
this.class= 123;
this.subject = "Arts";

displayInformation();

However when executing this code in Nodejs I am getting undefined in the last sentence:

student with 100 is Rajiv who is in class: 10 and he is of PCMC stream
student with 101 is Sam who is in class: 11 and he is of PCMB stream
student with 102 is Ghanshyam who is in class: 12 and he is of commerce stream
student with undefined is undefined who is in class: undefined and he is of undefined stream

Why is the result not the same in Nodejs and the Browser?

10
  • Please post your code with code formatting, the quotation formatting has wrapped all the lines and destroyed the indentation. Commented Apr 23, 2019 at 15:48
  • Paste your code, then use Ctl-k to mark it as code. Commented Apr 23, 2019 at 15:48
  • You don't have add > before every line. Paste your code, select it, click on the { } icon. Please read How to create a Minimal, Complete, and Verifiable example and How do I create a stack snippet? Commented Apr 23, 2019 at 15:51
  • I get student with 103 is Raju who is in class: undefined and he is of Artsstream in Chrome. You never assigned to this.class, so it's undefined. Commented Apr 23, 2019 at 15:51
  • I am sorry as i am new to stackoverflow, i will keep make sure this doesn't happens next time. but when i run the same code in VS code i am getting undefined for all the values, also after updating the value for class. Commented Apr 23, 2019 at 15:59

1 Answer 1

2

The last line actual result is:

student with 103 is Raju who is in class: undefined and he is of Arts stream 

The undefined is because you didn't assign a value to this.class

function displayInformation() {
  console.log("student with " + this.rollNo + " is " + this.name + " who is in class: " + this.class + " and he is of " + this.subject + " stream ")
};

this.name = "Raju";
this.age = 28;
this.rollNo = 103;
this.subject = "Arts";
this.class = 123; // <-- You missed this

displayInformation();

Also I really recommend to not use the this global object, I am not sure why are you using it.

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

3 Comments

when i run the same code in VS code i am getting undefined for all the values in the terminal, also after updating the value for class
Are you executing nodejs? or just a browser javascript file?
I just executed it in Nodejs and you are right, I am getting undefined in the terminal, if you let me I can edit your question.

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.