Oliver Nassar

Native/primitive types in JS

March 18, 2011

In working on something I call an 'Enforcer', which is to act like Type Hinting does in PHP for JS (either on client side or server side), I've had to come to terms with all the different primitive/native data types in JS. Here's a list I've compiled:

/**
 * __natives. A hash used for mapping primitive/native data types.
 *
 * @see     http://www.learn-javascript-tutorial.com/QuickRecap.cfm
 * @private
 * @var     Object
 */
var __natives = {
    "object": Object,
    "function": Function,
    "string": String,
    "number": Number,
    "boolean": Boolean,
    "null": null,
    "undefined": undefined,
    "date": Date,
    "array": Array,
    "regexp": RegExp,
    "error": Error,
    "typeError": TypeError,
    "htmlImageElement": HTMLImageElement,
    "htmlOptionElement": HTMLOptionElement
};

I'll keep adding to this post if I find more. The Error object is what sparked me to write this post, since it's something I didn't really think of at first.

Worth noting here is that the null and undefined data-types do not act like the rest; that is to say, you cannot access their constructors, and generally do not act like other Objects.