JavaScript Type Testing Utility

3.3.2 · maintenance · verified Sun Apr 19

The `is` library is a lightweight, comprehensive JavaScript utility designed for type and value testing. It provides a rich API to determine the type or specific characteristics of a variable, offering functions like `is.string()`, `is.array()`, `is.empty()`, `is.equal()`, and many more for common JavaScript types and states. Currently stable at version 3.3.2, the package maintains a steady but not rapid release cadence, primarily focusing on bug fixes and dependency updates. Its key differentiator is the extensive, declarative API that simplifies complex type checks, often replacing verbose `typeof` or `instanceof` chains, making code more readable and robust. It's widely used in both Node.js and browser environments for input validation, conditional logic, and defensive programming.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates importing the 'is' library and using various type-checking functions like `is.nil`, `is.string`, `is.array`, `is.object`, `is.empty`, `is.defined`, and `is.type` for conditional logic.

import is from 'is';

function processValue(value) {
  if (is.nil(value)) {
    console.log('Value is null or undefined.');
    return 'default_value';
  } else if (is.string(value)) {
    console.log(`Value is a string: ${value}`);
    return value.trim();
  } else if (is.array(value)) {
    console.log(`Value is an array with ${value.length} elements.`);
    return value.filter(item => is.defined(item));
  } else if (is.object(value) && is.empty(value)) {
    console.log('Value is an empty object.');
    return {};
  } else {
    console.log(`Value is of type: ${is.type(value)}`);
    return value;
  }
}

console.log(processValue(null));
console.log(processValue('  hello world  '));
console.log(processValue([1, undefined, 3]));
console.log(processValue({}));
console.log(processValue(123));
console.log(processValue(new Date()));

view raw JSON →