jQuery & jQuery UI Documentation

jQuery & jQuery UI

jQuery.type()

jQuery.type( obj ) Returns: String

Description: Determine the internal JavaScript [[Class]] of an object.

  • version added: 1.4.3jQuery.type( obj )

    objObject to get the internal JavaScript [[Class]] of.

A number of techniques are used to determine the exact return value for an object. The [[Class]] is determined as follows:

  • If the object is undefined or null, then "undefined" or "null" is returned accordingly.
  • If the object has an internal [[Class]] equivalent to one of the browser's built-in objects, the associated name is returned. (More details about this technique.)
    • jQuery.type(true) === "boolean"
    • jQuery.type(3) === "number"
    • jQuery.type("test") === "string"
    • jQuery.type(function(){}) === "function"
    • jQuery.type([]) === "array"
    • jQuery.type(new Date()) === "date"
    • jQuery.type(/test/) === "regexp"
  • Everything else returns "object" as its type.

Example:

Find out if the parameter is a RegExp.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
</head>
<body>
  Is it a RegExp? <b></b>
<script>$("b").append( "" + jQuery.type(/test/) );</script>

</body>
</html>