Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript Why use parentheses to seperate statement in if statements?

Norway123

New Coder
Hi. I started recently to read and solve excercises in the book "A smarter way to learn javascript". I have been working with javascript for around 2 months, but does not master the fundamentals yet, so I decided to give this book a shoot.

my question: I have been working with comparison operator in this book now and i find it a bit frustrating that the author of the book using parentheses after to seperate statements. You dont need to do that, does he use it only because it makes the code cleaner to read? I have been using codeacedemy and freecodecamp, and none of them using this.
for example:
if (a < b || (c > d || c === e))
if ((name === "Rob" || name === "Moskus") && age > 60)
if ((name === "Rob" && age > 60) || (name === "Moskus" || age < 6))

it works exactly the same way when writing in this way: if (a < b || c > d || c === e), if (name === "Rob" || name === "Moskus" && age > 60), if (name === "Rob" && age > 50 || name === "Moskus" || age < 6);
 
I think especially for the second and the last one it makes it easier to read (and maybe for the code to understand, idk).

If you don't have the parentheses, you might understand it as: if name = Rob is true and either of age > 60, name === "Moskus" or age < 6 is true, then run the function. That will be different from: if name = Rob and age > 60 is true or if either of name === "Moskus" and age < 6 is true, then run the function.

Idk if you understand what I mean?
 
I use parentheses so the thing makes sense to me - You don't need them for things to work you have them so if you need to troubleshoot or change things you don't waste a lot of time trying to figure complicated statements.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom