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?

Lolpez
  • 849
  • 8
  • 17
K.M.J
  • 105
  • 1
  • 2
  • 13
  • Please post your code with code formatting, the quotation formatting has wrapped all the lines and destroyed the indentation. – Barmar Apr 23 '19 at 15:48
  • Paste your code, then use `Ctl-k` to mark it as code. – Barmar Apr 23 '19 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](https://stackoverflow.com/help/mcve) and [How do I create a stack snippet?](https://meta.stackoverflow.com/questions/358992) – adiga Apr 23 '19 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. – Barmar Apr 23 '19 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. – K.M.J Apr 23 '19 at 15:59
  • How are you running this? From the console, or from a separate J's file? – Ferrybig Apr 23 '19 at 16:03
  • In firefox console this code works fine. – Matt Ellen Apr 23 '19 at 16:09
  • It looks like he is running it in Nodejs, I just tested it and I am getting undefined too. I edited the question and added the node.js tag – Lolpez Apr 23 '19 at 16:17
  • See [this](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) post. You can convert displayInformation to an arrow function and it will work as you want it to. – Mika Sundland Apr 23 '19 at 16:33
  • You can also look at [this](https://stackoverflow.com/questions/22770299/meaning-of-this-in-node-js-modules-and-functions) post. It is specific for Node. – Mika Sundland Apr 23 '19 at 16:40

1 Answers1

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.

Lolpez
  • 849
  • 8
  • 17
  • 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 – K.M.J Apr 23 '19 at 16:04
  • Are you executing nodejs? or just a browser javascript file? – Lolpez Apr 23 '19 at 16:05
  • 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. – Lolpez Apr 23 '19 at 16:08