Drycodez Posted August 13, 2011 Share Posted August 13, 2011 var name; i have seen varibles without value, but i just dont knw why its like that! I was told about it sometime ago, but i still dont understand it. var car; document.write(car)//prints: undefined. is it that they want UNDEFINED as the value or what? can somebody be kind enough to help me with this? Link to comment Share on other sites More sharing options...
jeffman Posted August 13, 2011 Share Posted August 13, 2011 The idea is usually to declare all your local variables at the top of your function, even if they cannot be assigned a value there. This makes sure that you as a developer don't forget to declare the variable locally and accidentally create a global variable later in the body of a function. In JavaScript, unlike other languages, there is no performance difference between declaring a var early or late in a function. It's all about helping you the developer stay organized. Link to comment Share on other sites More sharing options...
Drycodez Posted August 14, 2011 Author Share Posted August 14, 2011 var a=0; abc(); alert(a)function abc(){ var a=1;} that means the variable in the abc() function will be a local variable and will not overide the one in the body wright? vara=0; abc(); alert(a)function abc(){a=1;} this wil overide the variable in the body wright? Link to comment Share on other sites More sharing options...
thescientist Posted August 14, 2011 Share Posted August 14, 2011 yes, assuming you are declaring a in the global scope. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.