Correctly determining the type of a value in JavaScript is not very obvious, especially to beginners. However, using Object.prototype.toString
has always proven to yield a slightly better result for most values than the native typeof
operator.
Leveraging Object.prototype.toString
, we can create a better typeof
— a function called whatis()
, like so:
function whatis (value) {
return Object.prototype.toString.call(value)
.replace(/^\[object\s+([a-z]+)\]$/i, '$1')
.toLowerCase();
}
Here, we are doing 3 things:
First, call
Object.prototype.toString()
onvalue
to get the string tag of the value. The function returns a string of this format:"[object T]"
, whereT
is the string tag (e.g Array, String, WeakMap, etc).Next, use a regular expression and
String.prototype.replace()
to extract the string tag.Finally, change the string tag to all lowercase and return the result.
Note:
Thewhatis()
function is still error prone. Its type testing mechanism is not so reliable for some kinds of built-in or program defined objects, since there are several ways of changing the reported type of an object.
whatis(NaN); // 'number'
whatis(-5.23); // 'number'
whatis(Infinity); // 'number'
whatis(144705209n); // 'bigint'
whatis('hello world'); // 'string'
whatis(Symbol.iterator); // 'symbol'
whatis(null); // 'null'
whatis(false); // 'boolean'
whatis(undefined); // 'undefined'
whatis(/^.+$/); // 'regexp'
whatis([1, 2, 3, 4]); // 'array'
whatis({ x: 5, y: 10 }); // 'object'
whatis(function () {}); // 'function'
whatis(new Date()); // 'date'
whatis(window.location); // 'location'
whatis(document.body); // 'htmlbodyelement'
Note:
Thewhatis()
function will throw aReferenceError
for variables that have not been declared, unlike thetypeof
operator that evaluates to"undefined"
for undeclared variables.
console.log(typeof undeclaredVar); // "undefined"
console.log(whatis(undeclaredVar)); // Throws ReferenceError
Further Reading
- typeof - JavaScript | MDN
- JavaScript typeof: Understanding type-checking in JavaScript
- Understanding the JavaScript typeof Operator ← Alligator.io
- Typeof | freeCodeCamp Guide
❤️ Like and Share
If you found this post insightful in any way please:
- Like this post
- Comment your feedback
- Share with someone
- Follow me on Twitter
Top comments (8)
Why not add a test for
undefined
then?Adding a test for
undefined
doesn't still solve the undeclared variable problem. In JavaScript, as much as I know, accessing an undeclared variable will always throw aReferenceError
. The only exception being with thetypeof
operator, where it evaluates to"undefined"
.The
typeof
operator behaves such that it should never throw an error for any valid JavaScript value used as its operand, including an undeclared variable.That said, running the modified function you defined above with an undeclared variable will still throw a
ReferenceError
.Undefined
is a primitive type in Javascript. Understanding it and what it means is fundamental to the language. I'm very curious, why would you want to avoid returning it?The primary focus of this post is handling type checking in JavaScript. For a very long time, relying on the
typeof
operator has been very problematic, especially fornull
, arrays, undeclared variables, etc.For example, an undeclared identifier is not the same as a variable that is set to
undefined
. However, typeof operator reports both as"undefined"
. So how does one make the distinction? That is the essence of this post.The technique described in the post is very popular for detailed type checking. However, as mentioned in the post, it is not completely error proof.
Gotcha! Makes sense. Thanks for clarifying.
Not me that thinks this whole thing is surplus to requirements?
When you say requirements, what do you really mean?
just that I think for most purposes it's using an anvil when all that's needed is a hammer :)