npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

is-defined-type

v1.2.1

Published

Check if a variable in a nested object is defined and optionally also check its type. Useful for verifying API responses.

Readme

is-defined-type

Checks if a variable in a nested object is defined and optionally also check it's type.

This is especially useful for validating API responses which contain many nested objects. For example, if you expect a response like this:

var res = {
    first: {
        second: {
            third: "target"
        } 
    }
}

You could get "target" by using:

var val = res.first.second.third;

But if first, second or third is undefined or not an object you get an exception. So you can do some verbose checks:

var val = res && res.first && res.first.second && typeof res.first.second.third !== 'undefined' 
    ? res.first.second.third
    : null;

Or just use isDefinedType:

var val = isDefinedType(res,'first.second.third')
  ? res.first.second.third
  : null;

You can even make sure it is a string:

var val = isDefinedType(res,'first.second.third', 'string')
  ? res.first.second.third
  : null;

Pass along 'value' with the types to check and the value of the checked variable will be returned if it is defined and matches a given type

var val = isDefinedType(res,'first.second.third', ['string', 'value']) || null; 
// val === 'target'

Or specify a default value to be returned if the value is not defined

var val = isDefinedType(res,'first.second.third', 'string', 'Default'); 
// val === 'target'
var val2 = isDefinedType(res,'first.second.third.undefined', 'string', 'Default'); 
// val2 === 'Default'

Usage

isDefinedType(input, path, type, defaultValue);
  • input: Object to check
  • path: Optional path to key in input to check. Can be a string of keys with a dot between each level inside the object 'res.user.name' or an array of strings: ['res','user','name']. If any of the keys before the last one does not point to an object the function will return false.
  • type: Optional type which the last key in the path must match if it is not undefined. This must provided as single lowercase string or an array of lowercase strings if multiple types are valid. Possible values are: string | object | array | number | null. Null and array are treated as a special cases and will not match object. If 'value' is in type the function result will contain the checked variable if it is defined and matches a given type.
  • defaultValue: Optional default value which will be returned if path is not defined or does not match type. When defaultValue is not undefined will return value when path is defined and matches type

Installation

Browser

<script type="text/javascript" src="https://unpkg.com/is-defined-type">

Node.js

npm install is-defined-type --save
const isDefinedType = require('is-defined-type');

Examples

var data = {
    user: {
        name: 'Alice',
        email: null,
        credentials: {
            username: 'alice123',
            password: 'secret'
        },
        age: 10,
        aliases: [
            'foo',
            'bar'
        ]
    }
}

// Not nested
isDefinedType(data); // true
isDefinedType(undefined); // false

// Not nested with type check
isDefinedType(data, null, 'object'); // true
isDefinedType(data, null, 'string'); // false

// Nested
isDefinedType(data, 'user'); // true
isDefinedType(data, 'notUser'); // false

// Deeply nested
isDefinedType(data, 'user.name'); // true
isDefinedType(data, ['user','name']); // true
isDefinedType(data, 'user.credentials.username'); // true
isDefinedType(data, ['user','credentials','username']); // true
isDefinedType(data, 'user.unknown.username'); // false
isDefinedType(data, ['user','credentials','unknown']); // false

// Deeply nested with type check
isDefinedType(data, 'user.aliases', 'array'); // true
isDefinedType(data, 'user.age', ['number','null']); // true
isDefinedType(data, 'user.email','null'); // true
isDefinedType(data, 'user.email','object'); // false
isDefinedType(data, 'unknown.email','null'); // false

// Return default value
isDefinedType(data, 'user.name', 'string', 'Bob'); // Alice
isDefinedType(data, 'user.name', 'number', 'Bob'); // Bob