Skip to content

Instantly share code, notes, and snippets.

@getify
Forked from mikaelbr/label-arrow.js
Last active January 4, 2017 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getify/319e64fe362eb8c2132902c1794c14ae to your computer and use it in GitHub Desktop.
Save getify/319e64fe362eb8c2132902c1794c14ae to your computer and use it in GitHub Desktop.
// Doesn't throw an error, but doesn't return an object
const getObj = () => { a: 1 };
console.log(getObj()); //=> val 'undefined' : void
// Reason: It looks like an arrow function returning an object, but it isn't.
// It's an arrow function with a function body (containing a labeled
// statement) that returns nothing.
const getObj = () =>
{ // <- Start function body
a: // <- A label to use when break/continue; e.g. break a;
1 // <- Just a noop expression-statement, not doing anything.
// <- No return, meaning it implicitly returns `undefined`.
}; // <- End function body
// Note: Function body looks like a block statement, but it's not the same. A
// block statement would have returned the `1` value, but regular function
// bodies do not have implicit return.
// To actually return an object, turn into an expression instead of function body
const getObj = () => ({ a: 1 });
console.log(getObj());
//=> val { a : 1 } : { a: int }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment