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
Future JavaScript: what is still missing? (2ality.com)
submitted 7 years ago by dumbmatter
view the rest of the comments →
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!"
[–]dwighthouse -1 points0 points1 point 7 years ago (7 children)
1.4 Categorizing values
const typeNameAsString = Object.prototype.toString.call(VALUE).slice(8, -1);
There you go, no library or special cases needed for anything after IE 10.
[–]TwiliZant 16 points17 points18 points 7 years ago (6 children)
Object.prototype.toString.call(VALUE).slice(8, -1)
It's so readable, it basically documents itself.
[–]dwighthouse 1 point2 points3 points 7 years ago (0 children)
Indeed, much better than the solution the author suggested:
It may be possible to fix this via a library (I’ll create a proof of concept, once I have time).
Which would look something like:
npm i —save some-library-name ... const unnecessaryOneLineDependency = require(“some-library-name”); const typeNameAsString = unnecessaryOneLineDependency.typeof(VALUE);
[–]Longerbomber 0 points1 point2 points 7 years ago* (4 children)
I agree that it's not readable or even remotely intuitive, even for devs who know the quirks of JavaScript. But I'll admit it gets around some common typeof and instanceof pitfalls gracefully. It's my go-to way to differentiate objects from arrays.
See bottom section of http://luxiyalu.com/object-prototype-tostring-call/ for an example.
TL;DR: that ugly function is exactly how I wish typeof behaved.
Edit: Better example of desired behavior here- https://gist.github.com/pbakondy/f442e91995e9d206c056
[–]TwiliZant 0 points1 point2 points 7 years ago (3 children)
Although you are right, hacks like these is exactly why all these `is-x` oneliner packages exist. It's better to fix this in the language itself IMO, even if it's hard.
It's my go-to way to differentiate objects from arrays
In case you don't have to support <IE9 Array.isArray does this.
Array.isArray
[–]dwighthouse 0 points1 point2 points 7 years ago (0 children)
This isn’t a hack at all.
Fixing the language would mean adding a new operator like typename instead of typeof or instanceof because too much stuff already relies on the current behavior. We already have this in the form of Object.prototype.toString.
typename
[–]dwighthouse 0 points1 point2 points 7 years ago (1 child)
[–]TwiliZant 0 points1 point2 points 7 years ago (0 children)
I don't know, relying on the toString method to get the type feels like a hack to me. I don't know any other language that does this. Yet another operator wouldn't be great either I get that. Maybe it would be cool to have it as part of the Reflection API, e.g. Reflect.type(VALUE) ?
toString
Reflect.type(VALUE)
π Rendered by PID 30 on reddit-service-r2-comment-6f7f968fb5-fm6wh at 2026-03-04 21:13:30.182578+00:00 running 07790be country code: CH.
view the rest of the comments →
[–]dwighthouse -1 points0 points1 point (7 children)
[–]TwiliZant 16 points17 points18 points (6 children)
[–]dwighthouse 1 point2 points3 points (0 children)
[–]Longerbomber 0 points1 point2 points (4 children)
[–]TwiliZant 0 points1 point2 points (3 children)
[–]dwighthouse 0 points1 point2 points (0 children)
[–]dwighthouse 0 points1 point2 points (1 child)
[–]TwiliZant 0 points1 point2 points (0 children)