Javascript while loop variable -


i bit confused why mycounter = mycounter + 1; not require var declared before statement itself.

var mycounter=0; var linebreak='<br>';  while (mycounter <= 5) {     document.write('hello world' + linebreak);     mycounter = mycounter + 1; } 

the variable initialized on first line.

var mycounter=0; 

this same variable used other 2 lines reference it

while (mycounter <= 5) {     document.write('hello world' + linebreak);     mycounter = mycounter + 1; } 

if not initialized @ top (missing var keyword) considered 'global' variable. bad practice of course , should define variables in scope need them in. can put "use strict"; statement @ top of file throw exceptions when variables aren't defined.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -