0

I am trying to add a instance inside a function in javscript where i have observed the following behavior

function Calculator () {
    this.mm = 66;
    return 2 ;
}

var calculator = new Calculator();
console.log('  --> '+calculator.mm);   <<< gives output of 66

But the below code

function Calculator () {
    this.mm = 66;
    return {} ;
}

var calculator = new Calculator();
console.log('  --> '+calculator.mm);   <<< gives me undefined

I just want to know what different does returning an object from a primitive datatype make to a function . In other words why am i getting undefined here and not 66.

jayanthCoder
  • 615
  • 1
  • 6
  • 13

1 Answers1

-2

you should return the variable mm in the second one. Like return mm; That should solve your problem. Since you are not returning anything in the second one, that is why it is coming back as undefined.

adam.v
  • 1
  • 2