1

I have done a little test for a project of mine. I have made a variable inside a function, then in a different function I am trying to console log one of those functions. However, my problem is that it is saying X (my variable) is undefined.

I have researched this quite a lot and I haven't found any good answers, I have researched scopes and I have tried to make it corresponding to what I found under that, but even then it doesn't work.

function inputs(){
    var x = 19;
    return x;
}

function output(){
    console.log(x);
}

output();

I want it to log x to the console, but it says x is undefined.

Thank you in advance!

1 Answers1

0

You should be calling console.log(inputs()); so that the inputs() function returns the value of x which is then used by the output() function:

Since, x is declared (var x = 19;) inside inputs() so it cannot be access by the output()

function inputs() {
  var x = 19;
  return x;
}

function output() {
  console.log(inputs());
}

output();
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62