DEV Community

Cover image for Determining real data types in Javascript using Object.prototype.toString
Sunil Chaudhary
Sunil Chaudhary

Posted on

2 1

Determining real data types in Javascript using Object.prototype.toString

Is there an alternative (and maybe better way) to determine data type of variables in javascript other than typeof? Turns out there is one.

Recently, I was looking at some code and found a different way some developers were using to determine the data types using Object.prototype.toString instead of typeof. On further exploration, I found that Object.prototype.toString gives much better results as compared to typeof and can be useful at a lot of places.

Let’s look at some of the results it gives:

Javascript Datatype Value Object.prototype.toString typeof
Number 10 [object Number] number
BigInt 10n [object BigInt] bigint
Number new Number(11) [object Number] object
String 'medium' [object String] string
String '' [object String] string
Object new String('github') [object String] object
Object null [object Null] object
Undefined undefined [object Undefined] undefined
Boolean true [object Boolean] boolean
Object { site: 'medium' } [object Object] object
Object [1, 2, 3, 4, 5] [object Array] object
Object new Array(10) [object Array] object
Function () => {} [object Function] function
Symbol Symbol('medium') [object Symbol] symbol
Object new Date() [object Date] object
view raw Example.md hosted with ❤ by GitHub

Application

Though typeof works fine for most of the cases, toString will come in handy covering cases such as

  • we need to differentiate between various types in objects (such as arrays, null, object, date)
  • we need to get correct data type for primitive variables created using their respective object wrappers (e.g. new Number(10) is a number but typeof will give object)

Syntax

Basic Syntax

Object.prototype.toString.call(variable)

Example

Object.prototype.toString.call(10) //[object Number]
Object.prototype.toString.call([1,2,3,4,5]) //[object Array]
Object.prototype.toString.call({}) //[object Object]
Object.prototype.toString.call('github') //[object String]

One can also write a wrapper around it or even modify the function prototype to remove the unnecessary characters in the output and get only datatypes

function getType(value) {
    return Object.prototype.toString.call(value).slice(8).slice(0, -1).toLowerCase()
}

getType(10) //number
getType('github') //string
getType([1,2,3,4,5]) //array
getType({}) //object

Pros vs Cons

  • It seems typeof is more compact than toString in its usage as well as the result it returns but toString is more accurate.
    • toString gives more accurate data types which are useful when differentiating between various types of objects (arrays, null, objects, date)
    • toString gives more accurate results in cases if someone has used object wrapper for primitive data types such as new Number/String.
  • toString function can be overriden but typeof can’t which seems the only major drawback.

You can read more about the mechanism and working of the function over here.

Overall, Object.prototype.toString is a pretty good method to determine the datatypes correctly in lot of cases.


Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more