Return Early Pattern for Functions - "undefined"

Tell us what’s happening:

Your code so far

// Setup
function abTest(a, b) {
  // Only change code below this line

  if (a < 0 || b < 0) return undefined;

  // Only change code above this line

  return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));

// Change values below to test your code
**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36```.

**Link to the challenge:**

Just a reminder: you're not going to see the "undefined" returned in your CodeCamp output black dialog. If you open your browser console (Ctrl-Shift-J) on Chrome, for example, Cut and paste the code into the console. Then, enter. It will return undefined when either of your test values is negative.

Do you have a question?

Nope. Sorry, I thought we could share something we found helpful. I’ll make sure to stay away from that.

It’s not like it’s a bad thing to share useful tips, or sharing what cool thing you’ve just figured out, though posting the exact answer without spoiler tags is definitely frowned upon

Best of luck with the coding!

Hi there,

Seeing as there is no hint for this particular topic, I stumbled onto this answer which was helpful, but raised a question.
I tried this solution on my end before getting confused as to why it wasn’t working and then looking for an explanation on the forums. Reading this answer, I noticed that the big difference between my answer and this one is that there are no curly brackets after the if statement and before the return. I never even realised this was possible and don’t remember ever learning this to be possible.
Obviously, since we cannot change the code for the math return, the brackets following the if statement were throwing everything off balance, which is why my brain was breaking over this question in the first place.
My question is, how commonplace is it to use if statements without the curly brackets? And in the future, how would I judge whether or not to include them? Are they just not necessary if there is no following else if || else statement?

I hope my question is clear enough and is helpful to others.
Thank you

If statements execute the following statement, and the braces just denote a block statement

It’s part of C-like languages in general that you’re allowed to do this, there’s no reason the statement part of the if has to be a block statement

I should point out however that it only executes one statement, whether a block or not, and it’s considered by many to be very poor form

Actually at many of the stricter companies it would fail code review as a matter of principle, as it can lead to programmer error fairly easily

Imagine I’m in a rush to fix a bug in code someone else wrote:


and I don’t pay attention to the lack of braces and try to add another step beforehand:


now doTheThing() will be unconditionally called, every time, which could lead to some very horrible bugs dependent on what it does

That’s not just hypothetical, it has happened before and it’ll happen again:

Personally I’m very fond of them for early returns, but you should follow the style guide (if there is one) for your workplace, or consider perhaps following a common one at one of the large companies for your own projects if you haven’t settled on these things yourself

eslint can warn you of violations of some of the common style guides (google, airbnb) if you choose to use that