Type Checking
In a language with dynamic typing like JavaScript, it's often necessary (and a very good idea) to test the type of variable before performing any operations on it. Although it might not seem like much to test for the type of a variable, it isn't always a freebie, and in practice can simply result in annoyances and bugs because of subtle differences. Base provides a few handy functions to simplify the nuances entailed. Like the other issues we've touched on so far, there are subtleties amongst various browsers involving some of the finer points. The following list summarizes:
isString(/*Any*/ value)Returns
trueifvalueis aString.isArray(/*Any*/ value)Returns
trueifvalueis anArray.isFunction(/*Any*/ value)Returns
trueif value is aFunction.isObject(/*Any*/ value)Returns
trueifvalueis anObject(including anArrayandFunction) ornull.isArrayLike(/*Any*/ value)Returns
trueif value is anArraybut also allows for more permissive possibilities. For example, the built-inargumentsvalue that can be accessed from within a Function object is especially an oddball in that it does not support built-in methods such aspush; however, it is array-like in that it is a list of values that can be indexed.isAlien(/*Any*/ value)Returns
trueif value is a built-in function or native function such as an ActiveX component but does not respect the normal checks such as theinstanceof Function.
Duck Typing
A concept commonly involved in dynamic programming languages like Python ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access