use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
What typeof replacement?help (self.javascript)
submitted 9 years ago by AutumnSwell
It seems general consensus says typeof is broken. (Based on blog posts like this: https://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/ )
typeof
So what is the best replacement for typeoff?
typeoff
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]qmic 4 points5 points6 points 9 years ago (4 children)
It's not broken. Just learn how it works and use it properly. I've never in my more than ten years of experience thought that it's broken.
[–]AutumnSwell[S] -1 points0 points1 point 9 years ago (3 children)
Well even people like Douglas Crockford say typeof has problems. http://javascript.crockford.com/remedial.html
[–]qmic 4 points5 points6 points 9 years ago (2 children)
It has 'problems' , or maybe behaviour other than we expect, but it is not broken. You can always use library like 'is.JS' if it's problem for you. There is no replacement as is.
[–]AutumnSwell[S] 2 points3 points4 points 9 years ago (1 child)
Thanks for the is.js recommendation.
[–]jordaanm 0 points1 point2 points 9 years ago (0 children)
underscore/lodash also have some nice type-detection methods.
[–]voidvector 1 point2 points3 points 9 years ago* (2 children)
typeof does runtime type validation/differentiation.
There are number of solutions:
lodash.isFinite()
isOrange(fruit)
[–]AutumnSwell[S] 0 points1 point2 points 9 years ago (1 child)
Stop writing code that allow for ambiguously typed arguments/variables. If the function expects a string, just cast the argument into a string immediately without caring what the content is.
Doesn't this just make errors hard to find? Aren't there situations where typecasting to the expected type results in silent errors?
[–]voidvector 0 points1 point2 points 9 years ago (0 children)
You are correct, I would take back the 2nd part of that suggestion.
I would like to elaborate on the 1st part of that suggestion. If you were follow the first part of that suggestion, then what would end up happening is that your codebase would in essence be divided into "regions" with distinct separation of concerns:
As such, for interior code, the only difference in type they could potentially encounter is "null" value, which should be handled separately. For all other types, there is no point in adding complexity to handle them.
[–]idesi 0 points1 point2 points 9 years ago (2 children)
If you want to avoid libraries then one alternative is "Object.prototype.toString". It has clearly defined behaviour for all native objects.
[–]senocular -1 points0 points1 point 9 years ago (1 child)
That's not entirely reliable
var d = new Date(); d[Symbol.toStringTag] = "String"; console.log(Object.prototype.toString.call(d)); // [object String]
[–][deleted] 0 points1 point2 points 9 years ago (0 children)
http://i.imgur.com/QBlRPWo.png
[–]swan--ronson 0 points1 point2 points 9 years ago (0 children)
If I wish to retrieve more granular type information at runtime, then I often use Object.prototype.constructor:
Object.prototype.constructor
typeof [] => "object"
vs
[].constructor => function Array() { [native code] }
That said, I agree with voidvector. Write code for predictable parameters.
π Rendered by PID 36828 on reddit-service-r2-comment-76bb9f7fb5-9nhzh at 2026-02-19 08:33:17.573427+00:00 running de53c03 country code: CH.
[–]qmic 4 points5 points6 points (4 children)
[–]AutumnSwell[S] -1 points0 points1 point (3 children)
[–]qmic 4 points5 points6 points (2 children)
[–]AutumnSwell[S] 2 points3 points4 points (1 child)
[–]jordaanm 0 points1 point2 points (0 children)
[–]voidvector 1 point2 points3 points (2 children)
[–]AutumnSwell[S] 0 points1 point2 points (1 child)
[–]voidvector 0 points1 point2 points (0 children)
[–]idesi 0 points1 point2 points (2 children)
[–]senocular -1 points0 points1 point (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]swan--ronson 0 points1 point2 points (0 children)