3

In the following code, shouldn't x be considered a global variable? Thus, when it gets to the console.log line x it should have the value of "World". However, when I run this code it logs "Hello undefined". `

let x = "World";

function sayHello(x) {
    console.log("Hello ", x);
}

sayHello();

But when I change the parameter to y, it then works as I expected, logging "Hello World.

let x = "World";

function sayHello(y) {
    console.log("Hello ", x);
}

sayHello();

Can someone explain what is going on here?

thanks

kat1330
  • 5,134
  • 7
  • 38
  • 61
Coder12345
  • 43
  • 3
  • You simply have a global variable that you are attempting to use inside of function scope. Because it's a global variable, it can be accessed from anywhere. – Obsidian Age Apr 25 '18 at 23:44
  • 1
    This question is a million times better than the other one. – Patrick McElhaney Apr 25 '18 at 23:53
  • when you call `sayHello()`, you are passing nothing for the argument `x` that it expects in `function sayHello(x)`, so inside of that function, there *is* an `x`, but `x` is undefined. When you change it to `sayHello(y)`, then there is no `x` local to that function, so it looks at the parent scope, which does have an `x`, so it uses that. – dave Apr 25 '18 at 23:54
  • Nobody should be marking this as duplicate - if you do then you are being incredibly lazy and disingenuous. It is a clear question with a clear problem. The steps taken already have been clearly described. – Jordan Apr 26 '18 at 00:49
  • This is a duplicate of [this question](https://stackoverflow.com/q/41526721/5376789) so I don't vote to reopen it. – xskxzr Apr 26 '18 at 03:59
  • @dave: When you say "When you change it to `sayHello(y)`, then there is no `x` local to that function" - - isn't it the case that there _is_ an `x` local to that function, found in the `console.log("Hello", x);` statement? Thanks. – Coder12345 Apr 26 '18 at 18:51
  • the `x` in that case is not *local* to that function, it is defined outside of the function (but in a scope that the function has access to). If there was another `x` defined *inside* of that function, then when you do `console.log(x)`, the `x` would refer to the one inside of the function, not the one outside of it. – dave Apr 26 '18 at 20:03
  • @dave: So does the _parameter_ `x` in the `function sayHello(x)` make `x` local to that function? That is, does having the `x` in the parameter list essentially the same as defining `x` in the function? Otherwise, why would having the parameter as `y` produce a different outcome? – Coder12345 Apr 26 '18 at 21:07
  • 1
    that is correct – dave Apr 26 '18 at 21:08

2 Answers2

1

The x parameter in sayHello(x) shadows the global let x = "World";. If you pass a value in, sayHello("Hello"), you would get an output, Hello.

Vitruvie
  • 2,327
  • 18
  • 25
1

Try calling sayHello(‘Stack Overflow’). I’m sure you can guess whet the result will be.

The function is expecting x to be the first argument passed to the function. If no argument is passed, the argument x is undefined.

The technical term for what happened here is shadowing. The argument named x shadows the variable named x, even if no value is passed for the argument.

Patrick McElhaney
  • 57,901
  • 40
  • 134
  • 167