Drycodez Posted August 23, 2011 Share Posted August 23, 2011 if((A==1)&&(B==2)){return (true)} y is extra brackets surrounding the condiction and the true statement? Link to comment Share on other sites More sharing options...
jeffman Posted August 23, 2011 Share Posted August 23, 2011 None is required. You sometimes see that in conditions when multiple expressions are joined with && or ||, as here, to make it more readable. I see no use in surrounding a return value with (), but maybe some developers find it makes the expression more readable. Maybe it's required in other languages and they do it from force of habit? Link to comment Share on other sites More sharing options...
ShadowMage Posted August 23, 2011 Share Posted August 23, 2011 The parens in the condition are just a way to group statements. Sometimes, when and expression could be ambiguous, they are required (EDIT: That is, required in order to achieve expected results). Usually, though, it is just a matter of readability.This:if ((A==1)&&(B==2))is easier to read than:if (A==1&&B==2)because it clearly separates each condition that must be checked.As for the parens around true, I don't think there really is a purpose. Developer habit?Though it seems like it should return a boolean object (rather than a plain boolean value), similar to the way that ("a string").substr(2) works... Link to comment Share on other sites More sharing options...
Drycodez Posted August 23, 2011 Author Share Posted August 23, 2011 Group? {document.write('A'); alert('B');} the curly braces group the above statement 2geda! Does it mean dat ( and ) can do thesame? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 23, 2011 Share Posted August 23, 2011 Consider this:if (A == 1 && B == 2 || C == 3)You would probably want to explicitly group those so it's obvious whether you mean this:if ((A == 1 && B == 2) || C == 3)or this:if (A == 1 && (B == 2 || C == 3))And no, parentheses are not a replacement for curly brackets. Link to comment Share on other sites More sharing options...
thescientist Posted August 23, 2011 Share Posted August 23, 2011 Group? {document.write('A'); alert('B');} the curly braces group the above statement 2geda! Does it mean dat ( and ) can do thesame? that would only work in the context of function scope, there's a big difference for why and when the two are used (parens vs. braces). the parenthesis in the original context are just conveniences founded on the simple rules of order of operations; aka PEMDAS - parenthesis, exponents, multiplication, division, addition, subtraction. Link to comment Share on other sites More sharing options...
Drycodez Posted August 23, 2011 Author Share Posted August 23, 2011 Thanks i Link to comment Share on other sites More sharing options...
ShadowMage Posted August 23, 2011 Share Posted August 23, 2011 Consider this:if (A == 1 && B == 2 || C == 3)You would probably want to explicitly group those so it's obvious whether you mean this:if ((A == 1 && B == 2) || C == 3)or this:if (A == 1 && (B == 2 || C == 3))So I know I've got this right:The order of operations here goes from left to right, correct? So without parens, you would get the same behavior as this:if ((A == 1 && B == 2) || C == 3)Or is it the other way around? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 23, 2011 Share Posted August 23, 2011 The logical && and || operators do go from left to right, but && has a higher precedence than ||. It will evaluate the && before the ||, regardless of which one is farther left.https://developer.mozilla.org/en/JavaScript...ator_Precedence Link to comment Share on other sites More sharing options...
jeffman Posted August 23, 2011 Share Posted August 23, 2011 I always test the heck out of complex Booleans. Error console is good for that. Link to comment Share on other sites More sharing options...
Drycodez Posted August 24, 2011 Author Share Posted August 24, 2011 afta my experiment, i noticed the () is jux lyk d 1 dat preceed functions name. Its used to group statement (for readerbility) and to pass argument to what it precceed! Link to comment Share on other sites More sharing options...
justsomeguy Posted August 24, 2011 Share Posted August 24, 2011 I don't think I understood that, perhaps you could write that again using English. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.