The key goal is to fail gracefully and (among other things) avoid printing/using a variable that is undefined, null or empty.
Use ??= to allow zero (0) as a value for arithmetic functions.
Activities
Research:
How to test for an empty array?
How to test for an empty object?
How do utility libraries like Lodash or underscore test for emptiness?
Find a personal project, assignment or class example and try to break it with empty/falsy/null/undefined values. What’s the best method for fixing them?
2. Callback functions
Learning Objectives
Understand the difference between invoking a function and passing a function as a value.
Define callback function.
Define anonymous function.
Create a greet function that accepts a function as a parameter.
Pass an anonymous callback function using a function expression.
Terminology
Callback Function
A function passed to another function, presumably to be invoked later.
Anonymous Function
A function expression passed as a value with no name. This is how callback functions are often created.
The callback function is a general programming pattern that’s possible with first-class functions. There are many use cases in JS such as event emitters, array/object sorting, looping, etc.
If a function is only passed as a value once in your code, it’s often better to pass it as an anonymous function so that your code is in the same lexical location.
Activities
Try stepping into the callback functions using the VS Code Debug panel. Let Tony know if you can get it work (or not).
3. Handling events and event objects
Learning Objectives
Discuss Javascript events in terms of callback functions.
Log an event object to the console and inspect it.