JSTypes (no longer maintained)
JSTypes is a small JavaScript library that does two things:
- Check if variables are of certain types (numbers, strings, undefined)
- Reliably create certain values (
undefined
,NaN
,Infinity
)
Note that you probably shouldn't use JSTypes unless you need it -- other popular libraries like jQuery and Underscore already have many of these methods. If you aren't using these libraries, then JSTypes might be of help!
Examples:
var x = 1;
var y = new Number(22);
x instanceof Number; // false; not what we want!
JSTypes.isNumber(x); // true
JSTypes.isNumber(y); // true
var x;
JSTypes.isUndefined(x); // true
undefined = 10; // this is terrible code, but it might get written!
JSTypes.isUndefined(x); // still true
var x = 5;
undefined = 10;
x = undefined; // bad: x = 10
x = JSTypes.makeUndefined(); // good: x is now really undefined
JSTypes.isUndefined(x); // true
I recommend (and do) move this library into your code's namespace if you're writing a library. Just replace all occurrences of JSTypes
with your library's namespace.
For licensing info, see LICENSE.txt.
API
Here's everything in the API.
-
JSTypes.isNumber(toCheck)
returns true if something is a number -
JSTypes.isInteger(toCheck)
returns true if something is an integer -
JSTypes.isString(toCheck)
returns true if something is a string -
JSTypes.isBoolean(toCheck)
returns true if something is a boolean -
JSTypes.isArray(toCheck)
returns true if something is an array -
JSTypes.isDefined(toCheck)
returns true if something is defined (will return false forundefined
, ornull
, orNaN
) -
JSTypes.isUndefined(toCheck)
returns true if something isundefined
-
JSTypes.isNAN(toCheck)
returns true if something isNaN
-
JSTypes.isInfinite(toCheck)
returns true of something is positive or negative infinity -
JSTypes.makeUndefined()
always returnsundefined
, even if it's stupidly redefined -
JSTypes.makeNaN()
always returnsNaN
, even if it's stupidly redefined -
JSTypes.makeInfinity()
always returnsInfinity
, even if it's stupidly redefined