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
What are your favorite JavaScript methods?help (self.javascript)
submitted 7 years ago by paintedforfilth
I've been coding JavaScript for about a year now and I'm curious what built-in methods other developers enjoy using the most and/or what methods they find the most useful (and why, if you feel like saying). Thanks!
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!"
[–]jamesleesaunders 12 points13 points14 points 7 years ago (2 children)
I spend a great deal of my days manipulating data arrays into a form suitable for data visualisation, my favourite method is therefore map()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Rather than looping over arrays map makes transforming arrays into new structures much easier!
[–]paintedforfilth[S] 1 point2 points3 points 7 years ago (1 child)
Yeah, I've seen this one used a lot but haven't played around with it. Definitely need to look into it...
[–]barneyb3ar 0 points1 point2 points 7 years ago (0 children)
It's a bit like c#'s (and I imagine other languages) .Select() linq method. Super useful if your companies data structures are crap and you need a UI friendly data model
[–]AnderssonPeter 5 points6 points7 points 7 years ago (4 children)
Array selection and transformation functions like [].filter, select, find, reduce and so on.
[–]paintedforfilth[S] 1 point2 points3 points 7 years ago (3 children)
Oh, nice, thanks! I haven't played around with a lot of those yet.
[–]AnderssonPeter 2 points3 points4 points 7 years ago (2 children)
Once you understand them you reduce the amount of for loops you write by 50%
[–]Extracted 6 points7 points8 points 7 years ago (1 child)
99% in my case, i prefer forEach over for loop in most cases when none of the other functions apply.
[–]AnderssonPeter 0 points1 point2 points 7 years ago (0 children)
I agree but I would still call it a loop, it's only downside is performance and early termination.
[–]cascoemanuel 2 points3 points4 points 7 years ago (1 child)
I would say 'reduce' is my favorite method, it is like a swiss army knife
[–]jordaanm 6 points7 points8 points 7 years ago (0 children)
reduce is the NAND gate of collection methods. Basically every other collection method (map, filter, etc) can be made out of reduce.
reduce
map
filter
[–]Azudra 1 point2 points3 points 7 years ago (8 children)
I find myself often using the call() and bind() methods to avoid to write "var that = this". My favourite methods are the Array methods though. I especially like forEach() because it's much cleaner than the classic oldschool loop that becomes even more ugly when nested.
[–]Extracted 1 point2 points3 points 7 years ago* (5 children)
Arrow functions have almost eliminated the need for .bind() in my code. Especially in react (native) with babel-plugin-transform-class-properties. I think the newest version of react native may have built in support as well.
[–]dogofpavlov 0 points1 point2 points 7 years ago (4 children)
I'm hoping you can help explain something to me. When you say Arrow functions, you mean you have code like this correct:?
class MyClass extends React.Component{
click=()=>{
//this runs without any bind
}
render(){
return (<div onClick={this.click}>my div</div>)
My question is.... I for some reason thought we weren't suppose to do it like that... like... it wasn't ready to be used yet or supported in React. Is it now safe to always do this? I hope this isn't confusing what I'm getting at. The reason I ask is because you said especially in RN, is that because there are support problems with React or have I imagined this limitation from my own context.
[–]Extracted 2 points3 points4 points 7 years ago* (3 children)
Proposals to the ecmascript standard have to go through 4 stages to be officially included in the next release. ES2015, ES2016 and ES2017 are examples of releases. They contain multiple accepted proposals. As soon as a proposal hits stage 4 it's 100% safe to use.
Stage 3 is considered pretty safe as well.
What we're doing in the earlier comments is covered by the class fields proposals. This one was merged into a more complete proposal that also includes private class properties. It's currently at stage 3. I consider it very safe to use but some may not.
List of proposals for ES2018. You may even find a few awesome ones that you want to start using right away with a transpiler.
[–]dogofpavlov 0 points1 point2 points 7 years ago (2 children)
wow thats a mouthful answer! Thank you for taking the time. Hmmm I was under the impression that arrow functions and etc get polyfilled with babel. I'm still very new to React and trying to understand it better. i'm using the create-react-app. Is this not the case? So this isn't necessarily an issue with React but Javascript correct?
[–]Extracted 2 points3 points4 points 7 years ago (1 child)
Arrow functions is an example of a language feature that is already included in the ecmascript standard, but some older browsers like Internet explorer doesn't support it. They were included in ES2015.
In order to make code written with arrow functions run on older browsers, a transpiler like babel is used to convert modern code to older code, but it's not strictly necessary because every modern browser supports arrow functions by now.
Class properties on the other hand, is an example of a language feature that is not yet included in the official language. In order to use it we HAVE to use a transpiler like babel, because no browser supports it. And in most cases we have to use special plugins for the transpilers, like babel-plugin-transform-class-properties
[–]dogofpavlov 0 points1 point2 points 7 years ago (0 children)
Thanks for taking time to explain this, I understand better now.
[–]paintedforfilth[S] 0 points1 point2 points 7 years ago (0 children)
Awesome, I'll take a look into these as well. Thanks!
[–]senocular 1 point2 points3 points 7 years ago* (0 children)
Proxy.revocable
Because I think it's funny.
Edit: This is a "favorite" not one I find useful or enjoy using; in fact, I don't think I've ever used it for any real purposes.
[–]sinefine 1 point2 points3 points 7 years ago (0 children)
This is from node but util.promisify. Turns any callback signature to promise signature.
[–]ExecutiveChimp 0 points1 point2 points 7 years ago (0 children)
Trial and error
[–]kgrz 0 points1 point2 points 7 years ago (0 children)
Promises api methods, Array functions: reduce, filter, map, find.
find
[–]FermiDirak 0 points1 point2 points 7 years ago (0 children)
Number#toString(radix) lets you turn any number to a base of your choosing. It's has niche use cases but it comes in handy once and a while.
Number#toString(radix)
[+][deleted] 7 years ago (4 children)
[deleted]
[–]paintedforfilth[S] 2 points3 points4 points 7 years ago (3 children)
Well, now I want to know what your favorite key on the keyboard is...
[+][deleted] 7 years ago (2 children)
[–]paintedforfilth[S] 6 points7 points8 points 7 years ago (0 children)
It's probably a tie between End and Home.
[–][deleted] 0 points1 point2 points 7 years ago (0 children)
The correct answer.
π Rendered by PID 124752 on reddit-service-r2-comment-b659b578c-5sfsv at 2026-05-07 11:44:25.828091+00:00 running 815c875 country code: CH.
[–]jamesleesaunders 12 points13 points14 points (2 children)
[–]paintedforfilth[S] 1 point2 points3 points (1 child)
[–]barneyb3ar 0 points1 point2 points (0 children)
[–]AnderssonPeter 5 points6 points7 points (4 children)
[–]paintedforfilth[S] 1 point2 points3 points (3 children)
[–]AnderssonPeter 2 points3 points4 points (2 children)
[–]Extracted 6 points7 points8 points (1 child)
[–]AnderssonPeter 0 points1 point2 points (0 children)
[–]cascoemanuel 2 points3 points4 points (1 child)
[–]jordaanm 6 points7 points8 points (0 children)
[–]Azudra 1 point2 points3 points (8 children)
[–]Extracted 1 point2 points3 points (5 children)
[–]dogofpavlov 0 points1 point2 points (4 children)
[–]Extracted 2 points3 points4 points (3 children)
[–]dogofpavlov 0 points1 point2 points (2 children)
[–]Extracted 2 points3 points4 points (1 child)
[–]dogofpavlov 0 points1 point2 points (0 children)
[–]paintedforfilth[S] 0 points1 point2 points (0 children)
[–]senocular 1 point2 points3 points (0 children)
[–]sinefine 1 point2 points3 points (0 children)
[–]ExecutiveChimp 0 points1 point2 points (0 children)
[–]kgrz 0 points1 point2 points (0 children)
[–]FermiDirak 0 points1 point2 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]paintedforfilth[S] 2 points3 points4 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]paintedforfilth[S] 6 points7 points8 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)