-1

Was playing around with creating 'class objects'? if that is the correct term.

var cat = {
    eyes:2,
    pur: function() {
        console.log("puuuuurrrrr");
    }
};
cat.pur();

In Chrome this returns the console.log message and then on the next line undefined. Was wondering whats causing the undefined to pop up at the end. It doesn't do it when i call cat.eyes. In internet explorer this happens before the console.log() event.In nodeJS this happens after console.log.

bto.rdz
  • 6,636
  • 4
  • 35
  • 52
WouldBeNerd
  • 629
  • 11
  • 29

1 Answers1

1

you see first in the console what you wrote to the console which is puuuuurrrrr

the undefined is the output of pur() which is nothing meaning undefined.

if you would change pur to

console.log('whatever....');
return 'something';

than you will see instead of 'undefined' the value 'something'. hope that explains it.

sagie
  • 1,744
  • 14
  • 15
  • Thanks! Your answer really helped me understand. But i dont get how this relates to using 'console.log("hey")' outside of a function in the console. – WouldBeNerd Oct 15 '15 at 16:01
  • 1
    if you just write console.log('asdfasdf') in the developer tools console than it is running that script, and the output of that script is also printed. so it will print the console.log(whatever) than it will print the output of the line you wrote. the output of console.log is undefined so it will print undefined afterwards. – sagie Oct 15 '15 at 16:09