Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

Returns the name of the constructor used to create an objecta 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...

Code Block
themeRDark
languagejavascript
linenumberstrue
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...

Code Block
themeRDark
languagejavascript
linenumberstrue
// 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 your class, not "object"..the class.

Code Block
themeRDark
languagejavascript
linenumberstrue
function FooClass() {};
FooClass.typeof(); // "function"
typeof FooClass; // "function"
 
var foo = new FooClass;
foo.typeOf(); // "fooclassobject", because name of foo constructoris isan FooClassobject
typeof foo; // "object" because foo is an object
Div
classbox
Availability
Status
colourGreen
titleStable

Requires:

Div
classbox
Contents

Jump to:

Table of Contents
maxLevel5

Div
classbox

Type Checking

Topics:

Child pages (Children Display)
alltrue
depthall
pageType Checking
excerpttrue

Div
classbox

See also

Related articles:

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

Section
Column
width47%
Code Block
themeRDark
languagejavascript
titleJavascript's typeof operator
linenumberstrue
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"
Column
width53%
Code Block
themeRDark
languagejavascript
titletypeOf(stuff)
linenumberstrue
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"