0

How come this works and returns 123 twice:

function hello(){
  hello123 = 123

  console.log(hello123)
}

hello()
console.log(hello123)
  1. How come I don't need to declare the variable hello123 with var
  2. How come the variable persists outside the function? Is that because it's declared on window with the function being part of that scope?

Thanks

Mohamed El Mahallawy
  • 13,024
  • 13
  • 52
  • 84
  • Try [`console.log(window.hello123)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Global_variables) as well. – Jared Farrish Oct 20 '14 at 23:03
  • Tip: Use [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode). – Felix Kling Oct 20 '14 at 23:09

0 Answers0