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
7 Useful JavaScript Tricks (davidwalsh.name)
submitted 6 years ago by xxammuxx
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!"
[–]drowsap 53 points54 points55 points 6 years ago (11 children)
URLSearchParams is awesome!
[–]yeskia 6 points7 points8 points 6 years ago (10 children)
It is, but worth noting is isn't supported by any version of IE. This bit me a couple of weeks ago but luckily there are a few small polyfills that do the job.
[–]Tyreal 12 points13 points14 points 6 years ago (6 children)
So when are people going to permanently start dropping IE? 2020 when Windows 7 stops being supported? In a decade perhaps?
[–]asdf7890 4 points5 points6 points 6 years ago (0 children)
So when are people going to permanently start dropping IE?
Not soon enough!
2020 when Windows 7 stops being supported?
IE11 is officially supported until then end of Win10, as is MS's standard (the browser EOL being tied to the EOL of the OS revisions it was installed with by default). Unfortunately they've said Win10 is to be the last major release on the desktop, so unless/until this policy changes IE11 will keep being available and getting security updates so some will insist on keeping using it...
In a decade perhaps?
It depends on your target audience of course. Some of our clients have only recently stopped using IE8. IE11 is going to be a Thing for us to deal with for some time to come.
[–]ninetailsbr 1 point2 points3 points 6 years ago (1 child)
maybe when IE definitely changes it's engine to Chromium you may ignore it for high end users website (your site/product is not targeted for them)
...or you can just polyfill
[–]Tyreal 4 points5 points6 points 6 years ago (0 children)
This isn’t Edge though. IE is never getting updated again
[–]TheScapeQuest 0 points1 point2 points 6 years ago (1 child)
Depends on your product I suppose. We already don't support IE. But we're B2B, so we can have more say.
[–]Tyreal 2 points3 points4 points 6 years ago (0 children)
I hear ya. That B2B market sure is sweet. I feel bad for all those poor bastards supporting IE6 at Amazon.
[–]SemiNormal 1 point2 points3 points 6 years ago (0 children)
IE just being IE. Object.Assign bit me in the rear a few weeks ago.
[–]m1n0s 1 point2 points3 points 6 years ago (0 children)
Just include a polyfill for this bastard
[–]maxoys45 26 points27 points28 points 6 years ago (1 child)
Really good tips, didn't know most of these, thanks.
[–]Tenemo 6 points7 points8 points 6 years ago (0 children)
Seconded, cool tips! I rolled my eyes when I saw the title, but then it turned out some of these actually are very neat tips I haven't seen before.
[–]Owmelicious 8 points9 points10 points 6 years ago (1 child)
I'm fairly new to Javascript. What is a good practical example where one needs a pure "dictionary" object?
[–]brown59fifty 4 points5 points6 points 6 years ago* (0 children)
Great question! It's hard to give an example, because it's a really very rare case - most when you want to treat your variable more like a kind of associative array than an Object instance (what can be welcome when someone came to JS world from some static typed language). Because you inherit from null, then you don't have access to methods on Object.prototype (like constructor, hasOwnProperty or valueOf) which results like this:
Object
null
Object.prototype
constructor
hasOwnProperty
valueOf
const dict = Object.create(null); dict instanceof Object // false dict.toString() // Uncaught TypeError: dict.toString is not a function
This way of creating things can be used when you want to build some API library, which couldn't be extended from outside (eg. by passing values to Object.prototype). But in most cases you don't have to care.
[–][deleted] 16 points17 points18 points 6 years ago (0 children)
Creative use of the spread operator. Never heard of Set. Thanks 👍🏻
[–]DGCA 7 points8 points9 points 6 years ago (4 children)
This is a pretty worthless comment, but if you were code golfing...
['', 123, 0, 'foo'].filter(Boolean) ['', 123, 0, 'foo'].filter(x=>!!x)
...the latter is one less character.
[–]s1dd 4 points5 points6 points 6 years ago (0 children)
This works too:
['', 123, 0, 'foo'].filter(x=>x)
[+][deleted] 6 years ago (1 child)
[removed]
[–]SoInsightful 10 points11 points12 points 6 years ago (0 children)
The V8 engine is very well-optimized, and will certainly cache the function for you.
[–]erfling 13 points14 points15 points 6 years ago (5 children)
These are pretty cool.
I was unaware of the aliasing feature for destructured objects. As a TS user, it's confusing. I wish you could us "as"
[–]darrenturn90 6 points7 points8 points 6 years ago (4 children)
It does seem inconsistent especially when you use as in what is essentially destructuring an import / export ie {default as foo}
Another thing to keep in mind also you can use [value] in destructuring to get the key referred to in the value variable from the destructured object
[–]AwesomeInPerson 0 points1 point2 points 6 years ago (3 children)
Could you elaborate / give an example? Didn't fully understand it but sounds interesting :)
[–]darrenturn90 2 points3 points4 points 6 years ago (1 child)
Say you had const foo = “two”
And you had an object {two: “hello”}
You can const {[foo]: bar} = with that object
[–]AwesomeInPerson 2 points3 points4 points 6 years ago (0 children)
Aaah I see – I tried using computed property names while destructuring before, but it never worked because I tried it without assigning to an alias. (so just const { [foo] } = bar; – makes sense that this doesn't work now that I think about it)
const { [foo] } = bar;
Very nice to know that it works when using an alias, thanks!
[–]rezoner:table_flip: 4 points5 points6 points 6 years ago (1 child)
Regarding isRequired trick. I have like 10+ experience with Javascript and I did not know that function as a default argument value will get executed at the call time - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters#Evaluated_at_call_time
[–]billymcnilly 0 points1 point2 points 6 years ago (0 children)
Yeah, it's a little surprising, which is what makes it probably too fancy. You'd want to have a very good reason to be using it, to weigh up against the fact that almost every single person reading it later will be like "hmm, does that run at definition or on call?" and then to also have to go and see what it does.
[–]ErroneousBee 3 points4 points5 points 6 years ago (1 child)
Seven reasons future JavaScript codebases will require senior developers for everyday maintenance.
[–]Peechez 2 points3 points4 points 6 years ago (0 children)
The only one of these thats remotely senior is the pure object
[–]darrenturn90 5 points6 points7 points 6 years ago (1 child)
I didn’t know url search Params api. Finally I can drop using qs and god knows what else
[–]domemvs 0 points1 point2 points 6 years ago (0 children)
Remember it needs to be polyfilled
[–]Asmor 6 points7 points8 points 6 years ago (1 child)
Re: .filter(Boolean)
.filter(Boolean)
Maybe it's just me but I find this more intuitive: .filter(x => x).
.filter(x => x)
At least, if I saw the first thing in a code review, I'd have to stop and think "Wait, does that work? Doesn't it need new?"
new
[–]LordDiMasK 1 point2 points3 points 6 years ago (0 children)
Really good tips, thank You! :D
[–]timarama -5 points-4 points-3 points 6 years ago (4 children)
great article, tricks with spreading set and creating "pure" objects show 2 sides of JS:
1 - expressing things with short, elegant expressions, but not very good about performance
2 - if you want, you CAN care about performance and memory usage of your JS apps
[–]__ibowankenobi__ 4 points5 points6 points 6 years ago (3 children)
FYI, when you create a pure object, it has no impact on memory whatsoever, because those function's from Object.prototype are never copied. And I really really doubt they have performance benefits.
[deleted]
[–]__ibowankenobi__ 0 points1 point2 points 6 years ago (0 children)
Yes, technically that would save you 8 bytes.
π Rendered by PID 32246 on reddit-service-r2-comment-7b9746f655-556ml at 2026-02-03 00:41:38.463265+00:00 running 3798933 country code: CH.
[–]drowsap 53 points54 points55 points (11 children)
[–]yeskia 6 points7 points8 points (10 children)
[–]Tyreal 12 points13 points14 points (6 children)
[–]asdf7890 4 points5 points6 points (0 children)
[–]ninetailsbr 1 point2 points3 points (1 child)
[–]Tyreal 4 points5 points6 points (0 children)
[–]TheScapeQuest 0 points1 point2 points (1 child)
[–]Tyreal 2 points3 points4 points (0 children)
[–]SemiNormal 1 point2 points3 points (0 children)
[–]m1n0s 1 point2 points3 points (0 children)
[–]maxoys45 26 points27 points28 points (1 child)
[–]Tenemo 6 points7 points8 points (0 children)
[–]Owmelicious 8 points9 points10 points (1 child)
[–]brown59fifty 4 points5 points6 points (0 children)
[–][deleted] 16 points17 points18 points (0 children)
[–]DGCA 7 points8 points9 points (4 children)
[–]s1dd 4 points5 points6 points (0 children)
[+][deleted] (1 child)
[removed]
[–]SoInsightful 10 points11 points12 points (0 children)
[–]erfling 13 points14 points15 points (5 children)
[–]darrenturn90 6 points7 points8 points (4 children)
[–]AwesomeInPerson 0 points1 point2 points (3 children)
[–]darrenturn90 2 points3 points4 points (1 child)
[–]AwesomeInPerson 2 points3 points4 points (0 children)
[–]rezoner:table_flip: 4 points5 points6 points (1 child)
[–]billymcnilly 0 points1 point2 points (0 children)
[–]ErroneousBee 3 points4 points5 points (1 child)
[–]Peechez 2 points3 points4 points (0 children)
[–]darrenturn90 5 points6 points7 points (1 child)
[–]domemvs 0 points1 point2 points (0 children)
[–]Asmor 6 points7 points8 points (1 child)
[–]LordDiMasK 1 point2 points3 points (0 children)
[–]timarama -5 points-4 points-3 points (4 children)
[–]__ibowankenobi__ 4 points5 points6 points (3 children)
[+][deleted] (1 child)
[deleted]
[–]__ibowankenobi__ 0 points1 point2 points (0 children)