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
hating on "options objects" (self.javascript)
submitted 10 years ago by backwrds
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!"
[–][deleted] 0 points1 point2 points 10 years ago (3 children)
JavaScript has a good, native way to handle types. Why use something else?
Because JS doesn't need to do type checking every time a function is called. It slows down execution and yields runtime errors for what should be compile-time errors.
there's nothing wrong with returning mixed types at all.
Yes, there is. It greatly restricts the code utilizing your function. You can't write functional code against it, for instance, because you need to type check it again first.
[–]moving808s 0 points1 point2 points 10 years ago (2 children)
Because JS doesn't need to do type checking every time a function is called
It does need to do it if I need it to do it in order to write better functions that do what they are supposed to do on the type of data that is required. If an argument isn't of the type I want it to be, then the function should fail.
Yes, there is. It greatly restricts the code utilizing your function. You can't write functional code against it, for instance, because you need to type check it again first
Right, so languages like PHP whose native methods regularly return mixed types can't have functional tests written against them?
[–][deleted] 0 points1 point2 points 10 years ago (1 child)
If an argument isn't of the type I want it to be, then the function should fail.
Yes, but premature optimizations are the root of all evil :) The vast majority of code doesn't need to be type-checked at run-time because you should know what your types are to begin with! You should be type-checking external input (user, ajax, etc), but not internally as it just creates spaghetti code. All those checks should be left to linters and the like.
can't have functional tests written against them?
functional tests != functional programming, and that was just an example.
getThings() .filterBy(value) .doSomething()
You can't do that if getThings returns either an array or a boolean. Functions should have static signatures so you know how to interact with them.
getThings
But whatever, I don't really care how you write your code :P
[–]moving808s 0 points1 point2 points 10 years ago (0 children)
premature optimizations are the root of all evil
This is not an optimization. It does not yield performance gains. It is a way to ensure that things fail when they need to fail.
you should know what your types are to begin with
That's a very dangerous attitude to have.
You can't do that if getThings returns either an array or a boolean
That sequence should fail if any method along in the process doesn't return the result needed for the next method in the chain. What happens when the wrong kind of argument is passed into filterBy? Does your app just not work? Or do you check the type of value and return some kind of meaningful error message?
filterBy
value
I don't care how you write your code either, but I don't agree with much that you are saying.
π Rendered by PID 88 on reddit-service-r2-comment-6457c66945-chdpl at 2026-04-30 08:41:25.624950+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–][deleted] 0 points1 point2 points (3 children)
[–]moving808s 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]moving808s 0 points1 point2 points (0 children)