Skip to content

jQuery Type Detection — Complete Guide to $.type, $.isArray, $.isFunction, and More

DodaTech Updated 2026-06-28 5 min read

In this tutorial, you will learn about jquery type detection. We cover key concepts, practical examples, and best practices to help you master this topic.

jQuery type detection utilities provide reliable cross-browser methods for determining JavaScript value types, arrays, functions, objects, and numeric values, avoiding the pitfalls of the native typeof operator.

What You'll Learn

  • Using $.type() for accurate type detection
  • Checking for arrays, functions, and objects
  • Detecting empty objects and plain objects
  • Numeric validation with $.isNumeric()
  • Why jQuery's type detection is more reliable than typeof

Why It Matters

JavaScript's typeof operator has known quirks: typeof null returns 'object', typeof [] returns 'object', and typeof NaN returns 'number'. jQuery's type utilities normalize these inconsistencies.

Real-World Use

A data processing function that receives data from various sources (API, localStorage, user input) and needs to reliably determine whether the data is an array, object, string, or number before processing it.

Type Detection Flow

flowchart TD
    A[Value to Check] --> B[$.type()]
    B --> C{Result}
    C -->|'array'| D[Process as Array]
    C -->|'object'| E{$.isPlainObject?}
    C -->|'function'| F[Call Function]
    C -->|'number'| G[Format Number]
    C -->|'string'| H[Sanitize String]
    C -->|'null'| I[Handle Null]
    C -->|'undefined'| J[Use Default]
    style B fill:#e6f3ff,stroke:#4a90d9,stroke-width:2px

$.type() — Accurate Type Detection

console.log($.type('hello'));          // 'string'
console.log($.type(42));              // 'number'
console.log($.type(true));            // 'boolean'
console.log($.type(undefined));       // 'undefined'
console.log($.type(null));            // 'null' (not 'object'!)
console.log($.type([]));              // 'array' (not 'object'!)
console.log($.type({}));              // 'object'
console.log($.type(function() {}));   // 'function'
console.log($.type(new Date()));      // 'date'
console.log($.type(/regex/));         // 'regexp'
console.log($.type(new Error()));     // 'error'
console.log($.type(NaN));             // 'number' (same as typeof)
console.log($.type($));               // 'function'
console.log($.type($('.box')));       // 'object'

Expected output: $.type() returns accurate type names that match the actual type. Unlike typeof, it distinguishes null, arrays, dates, and regexps correctly.

$.type() vs typeof

// typeof pitfalls
console.log(typeof null);           // 'object' — wrong!
console.log(typeof []);             // 'object' — wrong!
console.log(typeof new Date());     // 'object' — correct but generic

// $.type fixes these
console.log($.type(null));          // 'null' — correct
console.log($.type([]));            // 'array' — correct
console.log($.type(new Date()));    // 'date' — correct

// $.type follows the internal [[Class]] property
// It works by calling Object.prototype.toString

$.isArray()

console.log($.isArray([]));             // true
console.log($.isArray([1, 2, 3]));      // true
console.log($.isArray({}));             // false
console.log($.isArray('string'));       // false
console.log($.isArray($('.items')));    // false (jQuery object, not array)
console.log($.isArray(null));           // false
console.log($.isArray(undefined));      // false

// In modern browsers, native Array.isArray() is equivalent
// jQuery's version works in older browsers too

$.isFunction()

function myFunc() {}
var arrowFunc = () => {};
var asyncFunc = async function() {};

console.log($.isFunction(myFunc));        // true
console.log($.isFunction(arrowFunc));     // true
console.log($.isFunction(asyncFunc));     // true
console.log($.isFunction(function(){}));  // true
console.log($.isFunction({}));            // false
console.log($.isFunction($));            // true (jQuery is a function)
console.log($.isFunction($.isFunction)); // true

// Useful for checking callback parameters
function processData(data, callback) {
  // Process data...

  if ($.isFunction(callback)) {
    callback(result);
  }
}

$.isNumeric()

console.log($.isNumeric(42));             // true
console.log($.isNumeric('42'));           // true (string number)
console.log($.isNumeric('42px'));         // false
console.log($.isNumeric(''));             // false
console.log($.isNumeric(true));           // false
console.log($.isNumeric(null));           // false
console.log($.isNumeric(NaN));            // false (different from typeof!)
console.log($.isNumeric(Infinity));       // false
console.log($.isNumeric(3.14));           // true
console.log($.isNumeric('3.14'));         // true
console.log($.isNumeric('-10'));          // true
console.log($.isNumeric('0xFF'));         // true (hex string)

// Best for user input validation
function parseNumber(input) {
  if ($.isNumeric(input)) {
    return parseFloat(input);
  }
  return 0; // Default
}

$.isEmptyObject()

console.log($.isEmptyObject({}));            // true
console.log($.isEmptyObject({ key: 'val' })); // false
console.log($.isEmptyObject([]));            // true (arrays are objects with no properties)
console.log($.isEmptyObject(new Date()));    // true (no enumerable properties)
console.log($.isEmptyObject(null));          // throws error (null has no properties)

// Check if an object has meaningful data
var response = {};

if (!$.isEmptyObject(response)) {
  console.log('Processing:', response);
} else {
  console.log('Empty response');
}

// Note: $.isEmptyObject checks own enumerable properties only

$.isPlainObject()

console.log($.isPlainObject({}));                 // true
console.log($.isPlainObject({ a: 1, b: 2 }));     // true
console.log($.isPlainObject(new Date()));         // false
console.log($.isPlainObject([]));                 // false
console.log($.isPlainObject(window));             // false
console.log($.isPlainObject(document));           // false
console.log($.isPlainObject($('.box')));          // false (jQuery object)
console.log($.isPlainObject(Object.create(null))); // true (no prototype)

// Useful for checking API responses
var data = JSON.parse(jsonString);
if ($.isPlainObject(data)) {
  // Safe to treat as a simple key-value object
  Object.keys(data).forEach(function(key) {
    console.log(key, data[key]);
  });
}

Practical Type Checking Function

function safeProcess(value, options) {
  options = options || {};

  switch ($.type(value)) {
    case 'string':
      return value.trim();

    case 'number':
      if (!$.isNumeric(value)) return 0;
      return value;

    case 'array':
      if (!$.isArray(value)) return [];
      return value.filter(function(item) { return item != null; });

    case 'object':
      if (!$.isPlainObject(value)) return {};
      return value;

    case 'function':
      if ($.isFunction(value)) {
        return value(options);
      }
      return function() {};

    case 'null':
    case 'undefined':
      return options.defaultValue || null;

    default:
      return value;
  }
}

// Usage
console.log(safeProcess('  hello  '));          // 'hello'
console.log(safeProcess('not a number'));        // 'not a number' (string case)
console.log(safeProcess(null, { defaultValue: 0 })); // 0

Common Mistakes

  1. Using typeof for array detection - typeof [] returns 'object'. Always use $.isArray() or Array.isArray() for arrays.

  2. Checking null with typeof - typeof null === 'object' is a famous JavaScript bug. Use $.type(null) which returns 'null'.

  3. Confusing $.isEmptyObject and $.isPlainObject - $.isEmptyObject({}) is true (no properties). $.isPlainObject({}) is also true (is a plain object). But $.isEmptyObject(new Date()) is true while $.isPlainObject(new Date()) is false.

  4. $.isNumeric('') returns false - Empty strings are not numeric. Always check for empty strings before numeric validation.

  5. Using $.isFunction on native methods - Native browser methods (like alert, console.log) may not pass $.isFunction in very old browsers. In modern browsers, they work fine.

Practice Questions

  1. What does $.type(null) return and why is it different from typeof null?
  2. How does $.isNumeric('42px') differ from $.isNumeric('42')?
  3. What is the difference between $.isEmptyObject and $.isPlainObject?
  4. Why should you use $.isArray instead of typeof for array detection?
  5. How would you check if a value is a function before calling it?

Challenge: Build a universal data sanitizer function that accepts any input and returns a cleaned version. Use $.type, $.isArray, $.isPlainObject, $.isNumeric, and $.isEmptyObject to handle strings (trim), numbers (validate), arrays (filter nulls), and objects (remove empty keys).

FAQ

Are jQuery type detection methods faster than native?

Native methods (Array.isArray, typeof) are faster. jQuery's methods add overhead for cross-browser consistency. Use native for performance-critical code.

Does $.type work with Symbols?

$.type(Symbol()) returns 'symbol' in jQuery 3.x. In older versions, it may return 'object'. Test your jQuery version.

Can I check for a jQuery object specifically?

Use value instanceof jQuery or check $.type(value) === 'object' && value.jquery. The .jquery property exists on all jQuery objects.

How do I check if something is a DOM element?

Check value instanceof Element or value.nodeType === 1. jQuery does not have a dedicated method for this.

Does $.isEmptyObject work on arrays?

Yes. It returns true for empty arrays because arrays have no own enumerable properties (only the indexed elements).

Mini Project

Build a data inspector tool that accepts any value (or JSON input) and displays its type, whether it is empty, whether it is a plain object, and its numeric validity. Use all the jQuery type detection methods and display the results in a formatted panel.

What's Next

Type detection ensures data quality. Learn how jQuery AJAX methods handle data from external sources and server responses.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro