(info) This AI is still in early stages of development.

typeOf()

Returns a more accurate object type name than Javascript's native "typeof" operator...

 

Syntax

typeOf() can be used as a global function, or as a method of any object instance...

var returnValue = typeOf(obj);
// or...
var returnValue = obj.typeOf();

Parameters

ParameterTypeMandatoryDescriptionAPI Version
objVariant(error)

By default, typeOf() will return the type of 'this' – the object that it is a method of.

If you pass in an object of any kind as a parameter, typeOf() will return the name of that object's constructor.

1.0

Return value

Returns the string name of the constructor of the class from which the object was instantiated.

(warning) If the object is a number, and it's value is NaN, typeOf() will return "nan" instead of "number".

Examples

Care must be taken when checking the type of primitive values...

// don't do this:
var returnValue = 5.typeOf(); // Syntax Error

// instead, do this:
var returnValue = typeOf(5); // "number"
var returnValue = (5).typeOf(); // "number"
 
// Or if you want a l33t way of doing it:
var returnValue = 5..typeOf(); // "number" (:

If you create a class and instantiate objects from it, typeOf() will return "object" not the name of the class.

function FooClass() {};
FooClass.typeof(); // "function"
typeof FooClass; // "function"
 
var foo = new FooClass;
foo.typeOf(); // "object" because foo is an object
typeof foo; // "object" because foo is an object
Availability STABLE

Requires:

Contents

Jump to:

Type Checking

Topics:

  • classOf()Returns the name of the class (constructor) used to create an object...
  • object.isFunctionA quick way to check if an object is a function...
  • typeOf()Returns a more accurate object type name than Javascript's native "typeof https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/typeof" operator...
  • object.isArrayA quick way to check if an object is an array...

See also

Related articles:

The following examples show the key differences between Javascript's typeof operator and the typeOf() function...

Javascript's typeof operator
typeof {a: 4}; //"object"
typeof [1, 2, 3]; //"object"
typeof function(){}; // "function"
(function() {
  return typeof arguments; //"object"
})();
typeof new ReferenceError; //"object"
typeof new Date; //"object"
typeof /a-z/; //"object"
typeof Math; //"object"
typeof JSON; //"object"
typeof NaN; //"number"
typeof 4; //"number"
typeof new Number(4); //"object"
typeof "abc"; //"string"
typeof new String("abc"); //"object"
typeof true; //"boolean"
typeof new Boolean(true); //"object"
typeof global //"object"
typeof null; //"null"
typeof undefined; //"undefined"
typeOf(stuff)
typeOf({a: 4}); //"object"
typeOf([1, 2, 3]); //"array"
typeOf(function(){}); // "function"
(function() {
  return typeOf(arguments); //"arguments"
})();
typeOf(new ReferenceError); //"error"
typeOf(new Date); //"date"
typeOf(/a-z/); //"regexp"
typeOf(Math); //"math"
typeOf(JSON); //"json"
typeOf(NaN)' //"nan"
typeOf(4); //"number"
typeOf(new Number(4)); //"number"
typeOf("abc"); //"string"
typeOf(new String("abc")); //"string"
typeOf(true); //"boolean"
typeOf(new Boolean(true)); //"boolean"
typeOf(global); //"global"
typeOf(null); //"null"
typeOf(undefined); //"undefined"