← Back to jQuery Course | Chapter 11: Utility Methods | Lesson 1 of 6

Type Checking Utilities

Type checking helps you find the type of a JavaScript value. jQuery provides useful utilities for this task.

What is Type Checking?

jQuery provides small utility functions like $.isArray() and $.isFunction() to reliably check a value's underlying JavaScript type, working around some of the quirks of the built-in typeof operator. This matters because typeof reports object for both plain objects and arrays, which isn't precise enough when your code needs to treat them differently.

Example: What is Type Checking?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      console.log($.isArray([1, 2, 3]), $.isFunction(function(){}));
    </script>
  </body>
</html>

Check Arrays

Arrays store multiple values in order, and you'll often receive data — from a form, an AJAX response, or user input — where you can't be sure in advance whether it's a single value or a list of them. Checking with $.isArray() before looping over a value avoids errors from calling array methods on something that isn't actually an array.

Example: Check Arrays

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      const data = [1, 2, 3];
      console.log($.isArray(data));
    </script>
  </body>
</html>

Check Functions

Functions contain reusable code, and you'll sometimes receive a value as an argument that's supposed to be callable, like an optional callback parameter. Checking its type with $.isFunction() before calling it prevents a runtime error if the caller passed something else, like undefined or a string, by mistake.

Example: Check Functions

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      function maybeCallback() { console.log("called"); }
      if ($.isFunction(maybeCallback)) {
      maybeCallback();
      }
    </script>
  </body>
</html>

Check Objects

Objects store related properties together, such as a user's name and email in one value. Type checking helps you confirm you're working with a genuine object — rather than null, an array, or a primitive — before you try to read properties off it.

Example: Check Objects

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      const value = {name: "Alice"};
      console.log($.type(value) === "object");
    </script>
  </body>
</html>

Practical Type Checking

Type checks become especially useful when the same variable could legitimately arrive in different shapes, such as an API that sometimes returns a single item and sometimes returns an array of items. Checking the type first lets your code branch and handle each shape correctly instead of assuming one form and breaking on the other.

Example: Practical Type Checking

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      function handle(data) {
      if ($.isArray(data)) {
      console.log("Got a list of", data.length);
      } else {
      console.log("Got a single item");
      }
      }
      handle([1, 2, 3]);
    </script>
  </body>
</html>
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.