jQuery Introduction | Utility Methods | Type Checking Methods $.type() $.isArray() $.isNumeric()

Utility methods

jQuery provides many utility methods that help with programming.

Type checking methods

jQuery provides the following methods that make it easy to check the type of a value passed as an argument.

Method Description
$.type() Compares the passed value with JavaScript’s internal class and provides more accurate type checking.
$.isArray() Checks whether the passed value is an array.
$.isFunction() Checks whether the passed value is a JavaScript function object.
$.isNumeric() Checks whether the passed value is a JavaScript Number object.
$.isEmptyObject() Checks whether the passed value is an empty object, meaning an object with no enumerable properties.
$.isPlainObject() Checks whether the passed value is a plain object.
$.isWindow() Checks whether the passed value is a JavaScript Window object.
$.isXMLDoc() Checks whether the passed DOM node is included in an XML document or is an XML document.

$.type() method

JavaScript’s typeof operator returns inaccurate or inconsistent type-checking results depending on the value.
However, jQuery provides the $.type() method, which compares the passed value with JavaScript’s internal class and provides more accurate type checking.

Example

$("#text")
    .append($.type(undefined) +"<br>")          // undefined
    .append($.type() +"<br>")                   // undefined
    .append($.type(window.notDefined) +"<br>")  // undefined
    .append($.type(null) +"<br>")               // null       
    .append($.type(true) +"<br>")               // boolean
    .append($.type(new Boolean()) +"<br>")      // boolean
    .append($.type(3) +"<br>")                  // number
    .append($.type(new Number(3)) +"<br>")      // number
    .append($.type("test") +"<br>")             // string
    .append($.type(new String("test")) +"<br>") // string
    .append($.type(function(){}) +"<br>")       // function
    .append($.type([]) +"<br>")                 // array
    .append($.type(new Array()) +"<br>")        // array
    .append($.type(new Date()) +"<br>")         // date
    .append($.type(new Error()) +"<br>")        // error - as of jQuery 1.9
    .append($.type(Symbol()) +"<br>")           // symbol - as of jQuery 1.9
    .append($.type(Object(Symbol())) +"<br>")   // symbol - as of jQuery 1.12
    .append($.type(/test/) +"<br>");            // regexp

Run code

Specific type checking methods

jQuery provides not only the $.type() method, but also several methods that check whether a passed value is a specific type.

Example

$("#text")
    .append($.isArray([]) + "<br>")                      // true
    .append($.isArray(10) + "<br>")                      // false
    .append($.isFunction(new Function()) + "<br>")       // true
    .append($.isFunction("string") + "<br>")             // false
    .append($.isNumeric(10) + "<br>")                    // true
    .append($.isNumeric([10]) + "<br>")                  // false
    .append($.isEmptyObject({}) + "<br>")                // true
    .append($.isEmptyObject({name: "text"}) + "<br>")    // false
    .append($.isPlainObject(new Object()) + "<br>")      // true
    .append($.isPlainObject(new Object("text")) + "<br>") // false
    .append($.isWindow(window) + "<br>")                 // true
    .append($.isXMLDoc(document) + "<br>");              // false

Run code

<!DOCTYPE html>
<html lang="en">
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script>
  $(function() {
    $("#text")
      .append($.isArray([]) + "<br>")                       // true
      .append($.isArray(10) + "<br>")                       // false
      .append($.isFunction(new Function()) + "<br>")        // true
      .append($.isFunction("string") + "<br>")              // false
      .append($.isNumeric(10) + "<br>")                     // true
      .append($.isNumeric([10]) + "<br>")                   // false
      .append($.isEmptyObject({}) + "<br>")                 // true
      .append($.isEmptyObject({name: "text"}) + "<br>")     // false
      .append($.isPlainObject(new Object()) + "<br>")       // true
      .append($.isPlainObject(new Object("text")) + "<br>") // false
      .append($.isWindow(window) + "<br>")                  // true
      .append($.isXMLDoc(document) + "<br>");               // false
  });
  </script>
</head>
<body>
  <h1>jQuery type check method</h1>
  <p id="text"></p>
</body>
</html>

References