Last active
September 26, 2017 16:41
-
-
Save mikaelbr/90629068e78b2abfa23318bda60d3dec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Doesn't throw an error, but doesn't return an object | |
const getObj = () => { a: 1 }; | |
console.log(getObj()); //=> val 'undefined' : void | |
// Reason: It looks lika an arrow function returning an object, but it isn't. | |
// It's an arrow function with statements returning nothing. | |
const getObj = () => | |
{ // <- Start function body | |
a: // <- Statement 1: A label to use when break/continue; e.g. break a; | |
1 // <- Statement 2: Just a noop expression statement, not doing anything. | |
// <- No return, meaning it implicitly returns `undefined`. | |
}; // <- End function body | |
// To actually return an object, turn into an expression instead of block statements. | |
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