Am I allowed to throw an error inside a ternary operator? Is this valid:
function foo(params) {
var msg = (params.msg) ? params.msg : (throw "error");
// do stuff if everything inside `params` is defined
}
What I’m trying to do is make sure all of the parameters needed, which are in a param object, are defined and throw an error if any one is not defined.
If this is just foolish, is there a better approach to doing this?
You could do this:
I wouldn’t really recommend it though, it makes for unreadable code.
This would also work (not that it’s really much better):
Or for a cleaner approach, make a named function.