Skip to content

Instantly share code, notes, and snippets.

@ryasmi
Last active August 7, 2018 16:10
Show Gist options
  • Save ryasmi/0dd657162eeee8f28b6063194aaedae1 to your computer and use it in GitHub Desktop.
Save ryasmi/0dd657162eeee8f28b6063194aaedae1 to your computer and use it in GitHub Desktop.
Code Habit - Const Functions
// 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);
// 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);
// 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