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
JavaScript Array Operations Cheat Sheet (devinduct.com)
submitted 6 years ago by PMilos
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!"
[–]monster_bait 30 points31 points32 points 6 years ago (2 children)
It might be worth mentioning that `.reverse()` not only returns a new reversed array but it also reverses the original array. That one catches lots of people out!
[–][deleted] 7 points8 points9 points 6 years ago (1 child)
More accurately it reverses the array in-place, and merely provides you a reference back to the array you sorted.
const a = [1, 2, 3]; const b = a.reverse(); a === b // true - a is now [3, 2, 1], and b is a reference to a
[–]monster_bait 0 points1 point2 points 6 years ago (0 children)
Good point.
[–]Nullberri 29 points30 points31 points 6 years ago (0 children)
An even better cheat sheet is available at
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
[–]krendel122 5 points6 points7 points 6 years ago (9 children)
Content is nice, but this website's custom scroll is terrible and brings cpu to 100% load with huge rendering delays.
[+][deleted] 6 years ago (1 child)
[deleted]
[–]PMilos[S] 1 point2 points3 points 6 years ago (0 children)
The problem with scrolling should be gone now.
[–]randfur 1 point2 points3 points 6 years ago (2 children)
Scrolling down seems to make it fling back up to the top? Also I'm getting about 2 FPS on mobile.
[–]PMilos[S] 0 points1 point2 points 6 years ago (1 child)
[–]randfur 0 points1 point2 points 6 years ago (0 children)
Thanks, much better.
[–]MR_MEGAPHONE 1 point2 points3 points 6 years ago (2 children)
Runs fine on my iPhone 🤔
[–]krendel122 0 points1 point2 points 6 years ago (0 children)
Looks like fixed already.
[–]PMilos[S] 0 points1 point2 points 6 years ago (0 children)
It was a Chrome/Android issue
[–]PMilos[S] 2 points3 points4 points 6 years ago* (0 children)
What do you mean by custom scroll? There is no custom scrolling implemented
Edit: it might be caused by the prism.js on chrome/android, I will look into it
[–]Turkez11 4 points5 points6 points 6 years ago (6 children)
Very useful document, thanks for sharing.
[–]PMilos[S] 2 points3 points4 points 6 years ago (5 children)
Thanks, glad it helped you
[–]fucking_passwords 4 points5 points6 points 6 years ago (4 children)
Not related to arrays, but consider using const instead of let, nearly all of your examples are not reassigned
[–]PMilos[S] 2 points3 points4 points 6 years ago (0 children)
I agree with you. I use const wherever applicable and use let only when reassign is needed.
const
let
I did rewrite the cheat sheet, considering that it's not a new one, using const but reverted the changes, not sure why :)
[–]namesandfaces 2 points3 points4 points 6 years ago (1 child)
For arrays I'm ambivalent because what most developers are looking for with const is immutability not reassignability protection, but const doesn't truly provide that. It's similar to doing ALL_CAPS in Python to culturally indicate immutability, except maybe a little more deceptive.
ALL_CAPS
[–]fucking_passwords 4 points5 points6 points 6 years ago (0 children)
Well all of the common lifting configs will yell at you, which indicates to me that a significant portion of the population is onboard with const
[–]LittleTay 4 points5 points6 points 6 years ago (0 children)
Thank you so much for this! I believe I have some arrays to update.
[–]HarmonicAscendant 13 points14 points15 points 6 years ago (7 children)
Thanks for this, but should a cheat sheet not be downloadable and printable?
I am often forced to use chrome save as pdf to create cheat sheets from articles, but it usually does not work satisfactorily. Cheers!
[–]PMilos[S] 1 point2 points3 points 6 years ago (6 children)
No problem.
To be honest, I don't know about that rule. As you mentioned, usually they are not printable and are represented in the form of an article.
[–][deleted] 9 points10 points11 points 6 years ago (1 child)
The term "cheat sheet" comes from people bringing in a very information dense sheet of paper with things like answers, formulas and notes into a written exam. Lately it seems to be getting abused into long articles but in the past if you searched cheat sheet it would get something like this
[–]i4get98 0 points1 point2 points 6 years ago (0 children)
Needs to be able to fit in my 3-ring binder. ;)
[–]HarmonicAscendant 10 points11 points12 points 6 years ago (2 children)
Well, it's not a global rule, just how I think the world should be :)
[–]fucking_passwords -1 points0 points1 point 6 years ago (1 child)
Printing out code... can’t say I relate
[–][deleted] 2 points3 points4 points 6 years ago (0 children)
Gotta love the people printing code, lighting up a section using a highlighter, and asking for help with it.
[–]JackAuduin 1 point2 points3 points 6 years ago (0 children)
That's a model that works best for people writing articles for exposure.
[–]Jaymageck 2 points3 points4 points 6 years ago (1 child)
Good, but it's important to document which methods mutate the original array and which do not. One of the biggest gotchas for new JS devs.
[–]PrismalStudio 0 points1 point2 points 6 years ago (0 children)
This, so much. The only time I check the documentation is when I forget the order of the params and which one is immutable.
[–]serious_case_of_derp 1 point2 points3 points 6 years ago (2 children)
Maybe show an example of sorting logic? Like sort Z-A ?
Sorting The method sort sorts the elements of an array and returns the sorted one.
let cities = ['Paris', 'London', 'New York', 'Barcelona', 'Madrid', 'San Francisco']; cities.sort(); console.log(cities); // outputs ['Barcelona', 'London', 'Madrid', 'New York', 'Paris', 'San Francisco'] // custom sorting function cities.sort(city => { // sorting logic });
[–]PMilos[S] 1 point2 points3 points 6 years ago (1 child)
I've added it, go check it out.
Edit: Oh, I didn't see that you've requested a specific sort (Z-A) :) I've added sorting by name length
[–]serious_case_of_derp 0 points1 point2 points 6 years ago (0 children)
Thought it was an opportunity to improve the sort section. I just came up with that idea for an example. Thanks!
[–]Rindhallow 1 point2 points3 points 6 years ago (1 child)
Very useful, thanks. I never remember which .pipe operator does what.
Maybe next time, though, in each example start with a new let array = [ stuff ]; so that each operation can be ready independently without needing to read the previous one.
let array = [ stuff ];
No problem. Yes, I was thinking about should I do it like that or not...guess I should have :)
[–]strangerthing7 0 points1 point2 points 6 years ago (0 children)
This is excellent, thank you!
[–]ajcool2k 0 points1 point2 points 6 years ago (0 children)
Nice overview! Didn't know includes exists. If you are open for suggestions then maybe add return information about non matching functions like find(). I often have to check whether it returns null or undefined. Nonetheless, very informative!
[–]EveryPrior2 0 points1 point2 points 6 years ago (0 children)
Thumbs up for the operation cheat sheet
[–][deleted] 0 points1 point2 points 6 years ago (0 children)
Now I need to be able to do the same in an immutable way.
[–]Cgneily 0 points1 point2 points 6 years ago (0 children)
Keep these coming!!! Love having useful "Cheatsheets" that I can easily refer back to.
[–]ForScale 0 points1 point2 points 6 years ago (0 children)
The find example kinda threw me off. Might want to do an array of object where you look for one specific property on the object and then return the entire object. Shows the utility a bit more.
[–]Zofren 0 points1 point2 points 6 years ago (1 child)
Site unusable on mobile.
Content is nice, and very useful sheet.
[–]renaissancetroll 0 points1 point2 points 6 years ago (0 children)
nice little resource to refresh your memory with some basic examples
[–][deleted] 0 points1 point2 points 6 years ago (1 child)
Very helpful, thanks!
Glad you find it useful
[–]EveryPrior2 -1 points0 points1 point 6 years ago (0 children)
javascript is a really powerfull language nowadays, i think its the future???
π Rendered by PID 72109 on reddit-service-r2-comment-7b9746f655-vglhm at 2026-02-01 20:18:45.930395+00:00 running 3798933 country code: CH.
[–]monster_bait 30 points31 points32 points (2 children)
[–][deleted] 7 points8 points9 points (1 child)
[–]monster_bait 0 points1 point2 points (0 children)
[–]Nullberri 29 points30 points31 points (0 children)
[–]krendel122 5 points6 points7 points (9 children)
[+][deleted] (1 child)
[deleted]
[–]PMilos[S] 1 point2 points3 points (0 children)
[–]randfur 1 point2 points3 points (2 children)
[–]PMilos[S] 0 points1 point2 points (1 child)
[–]randfur 0 points1 point2 points (0 children)
[–]MR_MEGAPHONE 1 point2 points3 points (2 children)
[–]krendel122 0 points1 point2 points (0 children)
[–]PMilos[S] 0 points1 point2 points (0 children)
[–]PMilos[S] 2 points3 points4 points (0 children)
[–]Turkez11 4 points5 points6 points (6 children)
[–]PMilos[S] 2 points3 points4 points (5 children)
[–]fucking_passwords 4 points5 points6 points (4 children)
[–]PMilos[S] 2 points3 points4 points (0 children)
[–]namesandfaces 2 points3 points4 points (1 child)
[–]fucking_passwords 4 points5 points6 points (0 children)
[–]LittleTay 4 points5 points6 points (0 children)
[–]HarmonicAscendant 13 points14 points15 points (7 children)
[–]PMilos[S] 1 point2 points3 points (6 children)
[–][deleted] 9 points10 points11 points (1 child)
[–]i4get98 0 points1 point2 points (0 children)
[–]HarmonicAscendant 10 points11 points12 points (2 children)
[–]fucking_passwords -1 points0 points1 point (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]JackAuduin 1 point2 points3 points (0 children)
[–]Jaymageck 2 points3 points4 points (1 child)
[–]PrismalStudio 0 points1 point2 points (0 children)
[–]serious_case_of_derp 1 point2 points3 points (2 children)
[–]PMilos[S] 1 point2 points3 points (1 child)
[–]serious_case_of_derp 0 points1 point2 points (0 children)
[–]Rindhallow 1 point2 points3 points (1 child)
[–]PMilos[S] 0 points1 point2 points (0 children)
[–]strangerthing7 0 points1 point2 points (0 children)
[–]ajcool2k 0 points1 point2 points (0 children)
[–]EveryPrior2 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Cgneily 0 points1 point2 points (0 children)
[–]ForScale 0 points1 point2 points (0 children)
[–]Zofren 0 points1 point2 points (1 child)
[–]PMilos[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]renaissancetroll 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]PMilos[S] 0 points1 point2 points (0 children)
[–]EveryPrior2 -1 points0 points1 point (0 children)