Why the return statement after arrow?

Tell us what’s happening:
In the getMousePosition example, there is no return statement in curly brackets after the arrow.

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

The challenge has us use { return { } } after the arrow statement. (as seen in the answer below)

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

My question is: Why is there { return {} } after the => , and not just ({ }) as seen in the getMousePosition example? What is the difference?

Thanks,

Noob.

Your code so far


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



Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15.

Challenge: Write Concise Object Literal Declarations Using Object Property Shorthand

Link to the challenge:

2 Likes

hmm good question, I’m new too. I’ve noticed is that if you bracket after the arrow => you get the same value as the return.

Maybe you return when you really want the function to end. If that makes sense.

You don’t have to use an explicit return in the challenge to pass it. That is the difference, one is implicit the other is explicit.

When you use arrow function, the return keyword is optional, as long your return statement is in one line.

This const sum = ((a,b) => a + b); is the same as const sum = ((a,b) => return a + b)

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums