difference between var x = 0 and x = 0

as we know in javascript there are so many ambiguity things. One of them is how to declare variable.

var x = 0

if you declare that on global, it will be global variable. If you put that on the function it will be local variable

x = 0

if you just simply put this anywhere. If you have declare it on global, var x, then that line would assign the x on global. But the dangerous thing is if you never declare it on global, that line will create global variable x so that this line would assign the global variable that have just been created.

So, to keep the consistency, Use var x = 0 to declare and x = 0 to assign!!

^^

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s