Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the best syntax to use for jQuery conditional statements?

I come from a Perl coders background, so out of habit I always use curly brackets to enclose the actions taken when a conditional statement returns a true. For example, and using jQuery, as this is where I need an explanation as to the difference, this two statements are both equivalent and of valid syntax.

if ($('#user_agreement').is(':checked')) { $('#thanks_message').show(); }  

if ($('#user_agreement').is(':checked')) $('#thanks_message').show(); 

Are my old habits betraying me, and I'm just writting unnecessary extra code without any advantage? Or is one method more effective, or considered more standard to good coding practices? I would really like to understand any differences between the two different syntactical approaches, especially when they both work just as well.

like image 464
Epiphany Avatar asked Sep 06 '13 07:09

Epiphany


People also ask

What is jQuery syntax?

The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the element(s). Basic syntax is: $(selector).action() A $ sign to define/access jQuery. A (selector) to "query (or find)" HTML elements. A jQuery action() to be performed on the element(s)

How do you use conditional statements in JavaScript?

In JavaScript we have the following conditional statements: Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false.

Can we use if condition in jQuery?

if (condition) statement; If condition is true, the statement will be executed and If condition is false, the statement will not be executed. We can use different relational operators in the If Statement.


2 Answers

I'm sorry to say, but this has nothing to do with jQuery. This is pure Javascript, through and through. But anyway, here's your answer:

They do the same thing.

HOWEVER.

  • The second option, without the curly braces, can only be used for one statement of code. As soon as the fist semi-colon is hit, the rest of the code is outside of the if statement.

  • The second option isn't even really frowned upon, but it really should be. It could save developers hours of looking through their code only to realize that they accidentally included a semi-colon right after the if statement. (i.e. they did something unfortunate like:

->

if ( $("#user_agreement").is(':checked')); $("#thanks_message").show();
  • The first option allows you to add more lines of code to the conditional statement without having to painstakingly add more curly braces.

  • Curly braces simply make your code clearer, especially as in the next bullet ->

  • And finally, nesting these thing becomes incredibly confusing. For example:

->

if(var1 == 'string1') 
    console.log("string1");
    if(var2 == 'string2')
        console.log("string2");
else 
    console.log("else");

The indentation indicates the else block goes with the outside if--but it actually goes with the inside if.

like image 100
mdenton8 Avatar answered Sep 28 '22 06:09

mdenton8


They are indeed both valid, tho I would personally advice for the usage of curly brackets. They make it clearer which lines are part of the if. This however is a very personal issue. Some people prefer to use them, others don't.

The advantage of placing the curly brackets is that your code can become clearer (but in that case, I would also advise the usage of newlines.

like image 29
MrP Avatar answered Sep 28 '22 07:09

MrP