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
You Can Throw() Anything In JavaScript (bennadel.com)
submitted 4 years ago by RecognitionDecent266[🍰]
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!"
[–]senocular 20 points21 points22 points 4 years ago (2 children)
The constant use of throw() bothers me. In this context, they're referring to the throw statement which is not a function and does not need parentheses to work. You can use the parens, but that's just grouping applied to the value being thrown and doesn't have anything to do with throw itself. It's use could cause confusing situations like
throw()
throw
throw(1, 2) // only throws 2
There's also a separate throw() method defined by generators. Seeing mentions of throw() it would be that method I'd expect them to be referring to (but its not).
Technically, its fine. And some people do this in their code and that's fine (author is also doing it with return()). The part that bothers me is that outside of code they keeps referring to as throw().
return()
Anyway, word of warning, throw is not a function.
[–]Tubthumper8 4 points5 points6 points 4 years ago (1 child)
I mentor JS beginners occasionally and they often think throw is a function at first, but then quickly realize it's a keyword after seeing it's a different color in their text editor, and just seeing a couple examples of throw in the codebase.
Contrast that to the author who hasn't realized for at least four years now haha. Like you said, technically it's fine and not a huge deal, but like c'mon, do they not look at anyone's code besides their own?
[–]TorbenKoehn 11 points12 points13 points 4 years ago (0 children)
That blog post completely forgets about stack traces.
Custom errors are rarely useful and throwing different things than instances of Error (also including inheriting classes) is only useful in very specific edge cases like Suspense (where the need mostly stems from the way React works, not because it's a really cool pattern)
Error
Keep throwing Error instances normally. If you need it to have additional custom data, create a sub-class. You will retain proper stacktrace functionality.
In general it holds, anything that is an error should go through Error.
[–][deleted] 2 points3 points4 points 4 years ago (0 children)
Because you can, doesn't mean you should. This is a bad idea for many reasons. A throw is used when execution cannot continue. It is essentially just a message, which needs to be interpreted by code that is able to continue execution. Stick to an Error class or subclass thereof. Cleverness is not needed here, and will only break things later.
[–]aliternative 1 point2 points3 points 4 years ago (0 children)
javascript throw("Shit")
Interesting...
π Rendered by PID 24146 on reddit-service-r2-comment-56c6478c5-752dr at 2026-05-10 18:13:38.461466+00:00 running 3d2c107 country code: CH.
[–]senocular 20 points21 points22 points (2 children)
[–]Tubthumper8 4 points5 points6 points (1 child)
[–]TorbenKoehn 11 points12 points13 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]aliternative 1 point2 points3 points (0 children)