To check what data type something has in javascript is not always the easiest. The language itself provides an operator called typeof for that which works in a straightforward way. Typeof returns a string of what a values data type is, so for an object "object" is returned and for a string "string".
However javascripts data types and the typeof operator aren't exactly perfect. For example for arrays and null "object" is returned and for NaN and Infinity "number". To check for anything more than just the primitive data types and to know if something's actually a number, string, null, an array or a real object a little more logic is required.
Table of contents
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 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.
Excellent and useful article!!!
Very helpful indeed! Thanks a lot. A lot of these should have something in native in JS I think.
Thanks !!
great overview, thanks for sharing!
I am trying to figure out if a function returns a string. But of course whenever I check typeof myCuteFunction it returns function of course.
Do you have an idea how to check which type a function returns?
regards
Kristian
If the function doesn't have side-effects, you can just call it and then test the return value. Since JavaScript isn't a strongly-typed language, there's no way of knowing what a function might return. It's possible a function could return different types depending on the input.
Nice. How to check unknown type? Is there a simple one line of code to check?
This was very helpful. Thanks for giving me a handy guide on Javascript datatypes.
Awesome guide, thanks. It's great the JavaScript community is taking on board type checking now. Really like TypeScript for this but when writing in Vanilla not always so straight forward. typeof really doesn't do the job…
Shared on twitter :)
For regex checking, we can ignore this "typeof value === 'object'" checking, right? since it's as value.constructor type checking.
Yes you can actually, it might be a bit overkill when I think about it.
Great stuff! For checking number, you could also just use Number.isFinite() instead right? Unlike isFinite(), that won't convert passed param to a number first, so it would have to already be a number and then you wouldn't need to check if the type was a number (true would only be returned if it was both a number and finite)
Yeah correct, with Number.isFinite you don't need any more logic. It's an es6 feature though so doesn't work in all browsers just yet.
very helpful, thanks
I turned this into a JavaScript module!
See: https://www.npmjs.com/package/typa
Cool!
What about JSON?
Hi have a look at this thread on Stack Overflow: stackoverflow.com/q/3710204. It has a couple different solutions for checking json.
Very useful, thanks for sharing
thank you very much
var b = new Boolean("true");
typeof b;
returns 'object'
Doesn't it need to be checked the same way as for String ( i.e. via instanceof)?
This helps me a lot.. Thank you..
Thanks! Very helpful
Hi glad you find it so!