Functions & Event Handling
Joshua Escobar
Button Mash!
What NOT to Do with JavaScript Functions
- Don't mutate global variables. Modifying global state inside a function makes code hard to track and debug.
- Avoid long functions. Break functions down if they handle too many tasks or are more than 20 lines.
- Stop using `var`. Always prefer `const` or `let` for variables within a function.
- Never forget `return`. If your function is meant to output a value, it must use the `return` keyword.
- Don't overuse anonymous functions. Named functions provide better stack traces for debugging.
- Avoid "magic" values. Don't use unexplained numbers or strings directly in your logic; use named constants instead.
- Limit arguments. If a function needs more than four arguments, pass a single configuration object instead.