bennyboywonder Posted March 4, 2007 Share Posted March 4, 2007 I just thought I would check something. Whenever I write an IF (or other control structure) if the resulting code is only one line, I tend not to bother with the curly braces, as it seems to make no difference as to how the code is executed. I have recently seen some examples written and noticed that these people have put the curly braces in regardless. Does this matter? e.g. is if(myval == 1) alert("myval is 1"); exactly the same as if(myval == 1) { alert("myval is 1"); } cos it certainly seems to run the same. Or is this just a conventions thing? Quote Link to comment Share on other sites More sharing options...
flappy_warbucks Posted March 4, 2007 Share Posted March 4, 2007 I just thought I would check something. Whenever I write an IF (or other control structure) if the resulting code is only one line, I tend not to bother with the curly braces, as it seems to make no difference as to how the code is executed. I have recently seen some examples written and noticed that these people have put the curly braces in regardless. Does this matter? e.g. is if(myval == 1) alert("myval is 1"); exactly the same as if(myval == 1) { alert("myval is 1"); } cos it certainly seems to run the same. Or is this just a conventions thing? i think it is just a conventions thing, i can understand it if you had to execute an entire block of code, but for just calling a function i would actually leave out eh braces. Quote Link to comment Share on other sites More sharing options...
bennyboywonder Posted March 4, 2007 Author Share Posted March 4, 2007 cheers Quote Link to comment Share on other sites More sharing options...
fenway Posted March 4, 2007 Share Posted March 4, 2007 NEVER LEAVE OUT THE BRACES... ever. Quote Link to comment Share on other sites More sharing options...
bennyboywonder Posted March 5, 2007 Author Share Posted March 5, 2007 why? Quote Link to comment Share on other sites More sharing options...
Greaser9780 Posted March 5, 2007 Share Posted March 5, 2007 I say one less thing to double check later if it's not working. Quote Link to comment Share on other sites More sharing options...
fenway Posted March 5, 2007 Share Posted March 5, 2007 why? Because it's lazy, and prone to errors... the worst is when people put it on another line, then add another statement, and don't realize that the 2nd statement will always run, becase it's not covered by the if(). Always use the braces -- JS, unlike Perl, does not have a true inline if, so you shouldn't code like it does. Quote Link to comment Share on other sites More sharing options...
bennyboywonder Posted March 5, 2007 Author Share Posted March 5, 2007 why? Because it's lazy, and prone to errors... the worst is when people put it on another line, then add another statement, and don't realize that the 2nd statement will always run, becase it's not covered by the if(). Always use the braces -- JS, unlike Perl, does not have a true inline if, so you shouldn't code like it does. but assuming I don't make what sounds like an *extremely stupid* mistake, then, it doesn't make any difference? Quote Link to comment Share on other sites More sharing options...
fenway Posted March 7, 2007 Share Posted March 7, 2007 Why would you try and save 2 keystrokes? Everyone accidentally makes cut & paste errors, etc... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.