Write Concise Object Literal Declarations Using Object Property Shorthand

Hello,

I’m trying to wrap my head around the parenthesis/curly brackets.

My code:

const createPerson = (name, age, gender) => {
  // Only change code below this line
  return ({
    name,
    age,
    gender
  });
  // Only change code above this line
};

The example shows :

const getMousePosition = (x, y) => ({ x, y });

but the answer for the problem is

  return {
    name,
    age,
    gender
  };

My brain is farting and I’m not quite understanding why there are (parenthesis) & {curly brackets} in the example, but no (parenthesis) in the answer (I hope my question was understandable :sweat_smile:).

It all depends upon if you want to use an implicit or explicit return. {} signals that you will not be using an implicit return, so you need to use () to fix that if you want to implicitly return an object literal.

1 Like

Sooooo {curly brackets} = explicit; (parenthesis) = implicit. Since the problem example is meant to “get Mouse Position”, the code is made to specify a single expression or basically implicitly/accurately return its exact coordinates (x, y).

The answer for this problem requires an explicit return, because it’s a group of statements (name, age, gender), and this needs to be expressed explicitly with {curly brackets}.

Is this correct???

1 Like

You could express both the example or the exercise with implicit or explicit return.

Lol dam this exercise. Still trying to wrap my head around this, but I’m guessing, because the exercise blocked our ability to make it into an implicit expression, we had to solve it as an explicit return with {curly brackets}?

1 Like

Exactly. That part was to remind you about the difference between the two.

2 Likes

:smiling_face_with_tear: I believe I understand it on a surface level, but will still be a while until I fully comprehend everything. Thank you, like always, for helping clear some confusion for me.

1 Like

I find that sometimes it’s best not to go too far down the rabbit hole when you’re trying to cram a lot of new information in. You will gradually pick up a deeper understanding as you learn anyway. Absorb what you can but keep the path ahead clear.

3 Likes

Hahahah easier said than done, especially when pre-conceived reasoning was based on poor logic

When using arrow functions, if you use {} after the =>, the return statement is used.

But to avoid having to write the return statement, you can simply use () which implicitly uses the return statement. This is used if you are just returning a single expression from your function.

1 Like

Haha thank you. I was struggling with this at first because I was definitely overthinking it. After Jeremy helped me out in understanding better, I was able to see that the answer to this exercise REQUIRED an explicit answer, since the given code included the curly brackets and blocked off a way to answer implicitly (with parenthesis).

const createPerson = (name, age, gender) => {

// Only change code below this line

  // Only change code above this line
};

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.