Last active
August 7, 2018 16:10
-
-
Save ryasmi/0dd657162eeee8f28b6063194aaedae1 to your computer and use it in GitHub Desktop.
Code Habit - Const Functions
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
// Preventing hoisting. We're used to reading top to bottom, so this can be quite confusing. | |
const result = add(1, 2); | |
function add(x, y) { | |
return x + y; | |
}; | |
console.log(result); |
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
// Consistency between declarations and expressions. It's much clearer that functions are just data. | |
const add = (x, y) => { | |
return x + y; | |
}; | |
const resultFromDeclaration = add(1, 2); | |
const resultFromExpression = ((x, y) => { | |
return x + y; | |
})(1, 2); | |
console.log(resultFromDeclaration); | |
console.log(resultFromExpression); |
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
// Avoiding reassignment. This can cause confusion if the reassignment on line 6 isn't spotted. | |
let add = (x, y) => { | |
return x + y; | |
}; | |
add = (x, y, z) => { | |
return x + y + z; | |
}; | |
const result = add(1, 2, 3); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment