Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to a variable without the "var" keyword inside a function?

function bla() { a=5; }

Does a automatically become a global variable?

And when exactly is it set? when the functions are being read for the first time and put into memory or only at the execution of the function?

like image 476
ilyo Avatar asked Jan 27 '26 05:01

ilyo


2 Answers

If you assign to a variable inside a function without declaring it with var then it becomes a global.

like image 73
JaredPar Avatar answered Jan 28 '26 20:01

JaredPar


The variable becomes a global when the function is first executed.

like image 23
Ned Batchelder Avatar answered Jan 28 '26 19:01

Ned Batchelder