-1

I wrote the below code but as expected it should give value as 4 but it is giving undefined. I am not able to understand why?

function outputInteger (a) {
 console.log(this.a);
}
outputInteger(4);

expected result : 4 given result : undefined

Thanks in advance.

SAMAR
  • 59
  • 1
  • 7
  • 1
    Possible duplicate of [How does the "this" keyword work?](https://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) – dave Jan 13 '19 at 07:40
  • Possible duplicate of [this inside function](https://stackoverflow.com/questions/1963357/this-inside-function) – Fullstack Guy Jan 13 '19 at 07:42

1 Answers1

1

You need to refer to your parameter a just by it's name:

function outputInteger (a) {
 console.log(a);
}
outputInteger(4);
Sami Hult
  • 2,869
  • 1
  • 9
  • 15
  • 1
    `this` is unrelated to the fact that functions are objects. The problem is rather that `this` inside a "normal" function call is meaningless. In strict mode the OP's code would actually throw an error. – Felix Kling Jan 13 '19 at 07:42
  • True, I'll remove that. – Sami Hult Jan 13 '19 at 07:43