Jump to content

quick question


bennyboywonder

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.