Skip to content

Instantly share code, notes, and snippets.

@2rohityadav
Last active July 16, 2021 05:28
Show Gist options
  • Save 2rohityadav/f9077c5e05457159d9caaa138a198a1f to your computer and use it in GitHub Desktop.
Save 2rohityadav/f9077c5e05457159d9caaa138a198a1f to your computer and use it in GitHub Desktop.
check data types in javascript

check data types in javascript

Table of Contents

String
Number
Array
Function
Object
Null & undefined
Boolean
RegExp
Error
Date
Symbol

String

A string is always a string so this one is easy. Except if called with new (new String) typeof will instead return "object". So to also include those strings instanceof can be used.

// Returns if a value is a string
function isString (value) {
return typeof value === 'string' || value instanceof String;
}

Number

From typeof more things than just an ordinary number will return "number" like NaN and Infinity. To know if a value really is a number the function isFinite is also required.

// Returns if a value is really a number
function isNumber (value) {
return typeof value === 'number' && isFinite(value);
}

Array

In javascript arrays are not true arrays

In javascript arrays are not true arrays like in java and in other languages. They're actually objects so typeof will return "object" for them. To know if something's really an array its constructor can be compared to Array.

// Returns if a value is an array
function isArray (value) {
return value && typeof value === 'object' && value.constructor === Array;
}

// ES5 actually has a method for this (ie9+) Array.isArray(value);

Function

Functions are functions so here just typeof is enough.

// Returns if a value is a function
function isFunction (value) {
return typeof value === 'function';
}

Object

Many things are objects in javascript. To know if a value is an object that can have properties and be looped through, its constructor can be compared to Object. It doesn't work for objects created from classes, then the instanceof operator can be used instead.

// Returns if a value is an object
function isObject (value) {
return value && typeof value === 'object' && value.constructor === Object;
}

Null & undefined

Most times you don't need to check explicitly for null and undefined since they're both falsy values. However to do it below functions does the trick.

// Returns if a value is null
function isNull (value) {
return value === null;
}

// Returns if a value is undefined
function isUndefined (value) {
return typeof value === 'undefined';
}

Boolean

For booleans typeof is enough since it returns "boolean" for both true and false.

// Returns if a value is a boolean
function isBoolean (value) {
return typeof value === 'boolean';
}

RegExp

RegExp's are objects so the only thing needed to check is if the constructor is RegExp.

// Returns if a value is a regexp
function isRegExp (value) {
return value && typeof value === 'object' && value.constructor === RegExp;
}

Error

Errors in javascript are the same as "exceptions" in many other programming languages. They come in a couple different forms like for instance Error, TypeError and RangeError. An instanceof statement is enough for them all, but just to be extra sure we also check for the "message" property that errors have.

// Returns if value is an error object
function isError (value) {
return value instanceof Error && typeof value.message !== 'undefined';
}

Date

Date isn't really a data type in javascript. But to know if something's a Date object it can be checked with instanceof.

// Returns if value is a date object
function isDate (value) {
return value instanceof Date;
}

Symbol

In ES6 the new datatype Symbol was added. Nicely enough typeof returns "symbol" for it so no more logic is required.

// Returns if a Symbol
function isSymbol (value) {
return typeof value === 'symbol';
}

That was all types for now. If you think something is wrong or missing and should be added, just leave a comment below! Some more in depth reading about javascripts data types and structures can be found at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment