0

When I run "addStuff" from the below code, I get two lines in my console (Chrome). The first line is "a" as we would expect, but the second line is "undefined". Why am I getting undefined?

Does every function return something, just when no "return" is explicitly defined, javascript gives undefined?

function Stuff() {
    this.x = 1;
    var x = "a";

    this.addStuff = function() {
        console.log(x);
    }
}

myStuff = new Stuff();
myStuff.addStuff(); // Outputs "a" on one line, then undefined on another line.
Don P
  • 60,113
  • 114
  • 300
  • 432

0 Answers0