127 Helpful JavaScript Snippets You Can Learn in 30 Seconds or Less — Part 1 of 6

JavaScript is one of the most popular languages you can learn

Fatos Morina
Sep 30 · 3 min read
Photo by Max Nelson on Unsplash

JavaScript is one of the most popular languages you can learn. As many people say: “If you’re going to learn just one programming language, go for JavaScript.”

Quincy Larson, the founder of FreeCodeCamp, was asked in a recent interview which language developers should learn first. He answered: “JavaScript.”:

“Software is eating the world, and JavaScript is eating software. JavaScript is becoming more dominant with each year, and nobody knows what might eventually replace it.

If you don’t have a very good reason to learn a new language (such as your job requiring you to maintain a non-JavaScript codebase), my humble advice is to focus on getting better at JavaScript.”

If this sounds compelling to you, here’s a list of 127 beneficial snippets taken from this project that you can learn and use immediately.


1. all

This snippet returns true if the predicate function returns true for all elements in a collection and false otherwise. You can omit the second argument fn if you want to use Boolean as a default value.


2. allEqual

This snippet checks whether all elements of the array are equal.


3. approximatelyEqual

This snippet checks whether two numbers are approximately equal to each other, with a small difference.


4. arrayToCSV

This snippet converts the elements that don’t have commas or double quotes to strings with comma-separated values.


5. arrayToHtmlList

This snippet converts the elements of an array into <li> tags and appends them to the list of the given ID.


6. attempt

This snippet executes a function, returning either the result or the caught error object.


7. average

This snippet returns the average of two or more numerical values.


8. averageBy

This snippet returns the average of an array after initially doing the mapping of each element to a value using a given function.


9. bifurcate

This snippet splits values into two groups and then puts a truthy element of filter in the first group, and in the second group otherwise.

You can use Array.prototype.reduce() and Array.prototype.push() to add elements to groups based on filter.


10. bifurcateBy

This snippet splits values into two groups, based on a predicate function. If the predicate function returns a truthy value, the element will be placed in the first group. Otherwise, it will be placed in the second group.

You can use Array.prototype.reduce() and Array.prototype.push() to add elements to groups, based on the value returned by fn for each element.


11. bottomVisible

This snippet checks whether the bottom of a page is visible.


12. byteSize

This snippet returns the length of a string in bytes.


13. capitalize

This snippet capitalizes the first letter of a string.


14. capitalizeEveryWord

This snippet capitalizes the first letter of every word in a given string.


15. castArray

This snippet converts a non-array value into array.


16. compact

This snippet removes false values from an array.


17. countOccurrences

This snippet counts the occurrences of a value in an array.


18. Create Directory

This snippet uses existsSync() to check whether a directory exists and then mkdirSync() to create it if it doesn’t.


19. currentURL

This snippet returns the current URL.


20. dayOfYear

This snippet gets the day of the year from a Date object.


21. decapitalize

This snippet turns the first letter of a string into lowercase.

This is part one of these short snippets. There are five other parts remaining that you can read.

Fatos Morina

Written by

Data Scientist | Software Engineer | Writer at https://www.fatosmorina.com/

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade