
filter(exists) // maybe multiply the contents of an array by 2, // default to 0 if the array is empty const maybeDouble = maybe(0, x => x * 2) const emptyArray = toMaybeArray(null) const maybe2 = toMaybeArray(2) // logs: "maybeDouble with fallback: 0" console.log('maybeDouble with fallback: ', maybeDouble(emptyArray)) // logs: "maybeDouble(maybe2): 4" console.log('maybeDouble(maybe2): ', maybeDouble(maybe2)) For example, I’ll use redux action creators that can handle undefined values to hydrate user records: const setUser = () => arr => arr.map(f) || fallback // turn a value (or null/undefined) into a maybeArray const toMaybeArray = value =>. I always pass inputs I receive from the network, database, or user input through a hydrating function. For example, check out react-jsonschema-form. I often rely on schema validators to help with that job. When you’re dealing with user input, validation is the first and best line of defense.

Here are some of the most common sources: To get a better handle on this problem, we need to understand where these values can come from. In some statically typed languages, you can say that null and undefined are illegal values, and let your programming language throw a TypeError at compile time, but even in those languages, that can't prevent null inputs from flowing into the program at runtime. Some languages have built-in affordances for those circumstances. What are the best strategies to minimize errors caused by values that could be null, undefined, or otherwise uninitialized at runtime? One aspect of JavaScript development that many developers struggle with is dealing with optional values.
