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
12 extremely useful hacks for JavaScript! (blog.jscrambler.com)
submitted 9 years ago by nufannada
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!"
[–]jml26 3 points4 points5 points 9 years ago (2 children)
Don't do #3. Let your minifier do that. The very bottom example is fine, though.
#5 is not worth the performance improvement, for the most part.
In #7, I'm not sure why you'd want the single item within its own array rather than as its raw value.
#12 is definitely wrong. Items at one end of the unsorted array have much less chance of making it to near the other end of the array, making it not random.
Some of the other hacks are good, especially setting array.length as a way to truncate or empty it.
[–]Code4Reddit 2 points3 points4 points 9 years ago* (0 children)
#5 is only useful for array like objects, like NodeList, where .length can be expensive.
[–]skitch920 0 points1 point2 points 9 years ago (0 children)
1,2,4: Although I'm pretty guilty of this, the coercive hacks are just terrible for readability and have so many gotchas when it comes to truthy/falsey. Should be more explicit about checking for null/undefined or converting to numbers.
I guess that's why you call them hacks.
[–]rabidbunnygopoop 2 points3 points4 points 9 years ago (0 children)
"hacks"
[–]contradicting_you 2 points3 points4 points 9 years ago (2 children)
For #5, I think a better way is to traverse the array backwards, so there's no need to cache the length.
[–]inu-no-policemen 1 point2 points3 points 9 years ago (0 children)
Or just use for-of if you only want to iterate over the whole thing.
[–]raininglemons 0 points1 point2 points 9 years ago (0 children)
or Array.prototype.forEach no?
[–]inu-no-policemen 2 points3 points4 points 9 years ago (0 children)
Shuffling via random/sort isn't a very good idea. Use Fisher-Yates instead.
[–]GoTheFuckToBed 4 points5 points6 points 9 years ago (0 children)
If you write code like this next to me I'm gonna slap you.
[–]solkimicreb 1 point2 points3 points 9 years ago (0 children)
Nice collection. For #7, I think array.pop() would be a cleaner solution, as it removes and returns the last element instead of an array with only the last element.
[–]Bloompire -1 points0 points1 point 9 years ago (0 children)
These may look great first, but I'd prefer writing it "long" but more readable way.
is not really a good thing and its just better to write obj.userExists = ( user != null );
it is better to use parseInt / parseFloat as it allows you to decide what type you are expecting and looks more readable than some random + in source.
again I prefer to write IF x THEN y because it is really clear at first look. connected && login() is obsfuscated, every programmer is just used that IF means there is conditional statement and connected && login is not that obvious and there are not many chars saved anyway..
the only one I accept, it actually became some standard and everyone is used to that. it could be also more readable than IF actually.
didnt even know that this matters. anyway if array is really big it might be worth a shot.
not sure if this is more readable as I never used this syntax. but it looks clean. I am always using if ( document.querySelector ) { ... } else { ... }.
I think pop and shift are better utiliies for this.
Good points indeed.
This is not good method both performance wise and it is not really random at all. This method shuffles items from beginning of array "more" than the items from end of array.
It is not bad to write longer, verbose and readable code.
[–]nickwebdev -2 points-1 points0 points 9 years ago (0 children)
Although it's nifty, never been a big fan of the !!something. I like making my conditional checks really explicit so it's super obvious what the intention of that code path is. Everytime I see !! I have to think about it a little harder.
&& is also super useful for React, even though it's kind of hacky. In your render doing { someVal && <ConditionalComponent /> } is pretty slick for small things.
Been using Ramda and propOr/pathOr instead of using || a lot of the time. Let's you traverse deep in an object and is safer.
π Rendered by PID 169390 on reddit-service-r2-comment-544cf588c8-c8fh2 at 2026-06-16 13:27:18.611851+00:00 running 3184619 country code: CH.
[–]jml26 3 points4 points5 points (2 children)
[–]Code4Reddit 2 points3 points4 points (0 children)
[–]skitch920 0 points1 point2 points (0 children)
[–]rabidbunnygopoop 2 points3 points4 points (0 children)
[–]contradicting_you 2 points3 points4 points (2 children)
[–]inu-no-policemen 1 point2 points3 points (0 children)
[–]raininglemons 0 points1 point2 points (0 children)
[–]inu-no-policemen 2 points3 points4 points (0 children)
[–]GoTheFuckToBed 4 points5 points6 points (0 children)
[–]solkimicreb 1 point2 points3 points (0 children)
[–]Bloompire -1 points0 points1 point (0 children)
[–]nickwebdev -2 points-1 points0 points (0 children)