0

The script below seems to be rendering different results in IE and Chrome. I know the code is wrong as I'm calling name in a different scope. I'm also expecting it to log undefined. So yes, I'm just not sure about this difference in the behavior and why?

var obj = {
  testA: function() {
    var name;
  },
  testB: function() {
    console.log(name)
  }
}

obj.testB()

When testB() is called in EDGE and Chrome, I'm getting different log results:

  1. The log is an empty string ("") - CHROME List item
  2. The log is an empty object {} - EDGE enter image description here
mplungjan
  • 155,085
  • 27
  • 166
  • 222
RJDO
  • 65
  • 1
  • 1
  • 7
  • Visit the [help], take the [tour] to see what and [ask]. If you get stuck, post a minimal reproducible example of your attempt, noting input and expected output using the `[<>]` snippet editor. – mplungjan Feb 16 '22 at 07:47
  • 4
    Why do you expect `undefined`? `var name` is out of scope; it would throw an error if it wasn’t the global property [`window.name`](//developer.mozilla.org/docs/Web/API/Window/name). – Sebastian Simon Feb 16 '22 at 07:48
  • I don't get that result from Edge. The only reason I could see for it would be if the name of the window in which you're running this code is `"{}"`, which is possible but unlikely. The only difference I see between Chrome and Edge in a normal window (which has no name) is that Chrome shows a blank line in the console for `console.log("")` and Edge doesn't (which is weird, but I double-checked it and that's what it does). – T.J. Crowder Feb 16 '22 at 07:53
  • But as @SebastianSimon pointed out, `name` in that code is the `name` global, nothing to do with the `var name` in `testA`. – T.J. Crowder Feb 16 '22 at 07:54

0 Answers0