When coding a library in JavaScript, what is the most standard (friendliest?) way to handle invalid input to a function? My gut tells me that returning undefined is perfectl
Advantages of exceptions:
Disadvantages of exceptions:
maybe obvious If you extending an environment - continue their practice as a minimum
Quick answer. If this is javascript in the browser undefined is OK, if it is javascript in the server throw an error.
Improved Let the library user select the behavior as an option, either library global, or on an object by object basis.
I think undefined
is fine but keep in mind that:
JavaScript does not have a void type, so every function must return a value. The default value is undefined, except for constructors, where the default return value is this.
So you don't need to explicitly returns undefined. It will be by default.
see http://javascript.crockford.com/survey.html