Alright, we can now create a salad:
Very good. Now what happens if we call
makeSalad(..) without any ingredients? Or if we call
makeSalad(..) with something else than ingredients? Well let’s see.
Indeed. Because we did not pass
ingredients, its value is
undefined which does not have a
join(..) function. A simple fix would be to set a default value for
Now we can safely make our salad without passing any ingredients without risking a script crash. It will return an empty string. Given that the function usually returns a string, that seems like a valid way of handling this edge case.
Now what about the case where we pass something else than an array?
ingredients is an object and an object does not have a
join(..) function. To prevent this code from failing, we could add an extra check to our function to make sure that the given arguments is an array.
In this case, the script will still throw an error but the error will be very explicit, which is much better for catching and debugging.
Long story short: handle edge cases in your functions. Don’t assume anything, and provide meaningful fallbacks and errors.