11 JavaScript Tricks You Won’t Find in Most Tutorials
Useful tips for writing more concise and performant JavaScript
When I began learning JavaScript, I made a list of every time-saving trick that I found in other people’s code, on code challenge websites, and anywhere other than the tutorials I was using.
I have been contributing to this list since then, and in this article, I’ll share 11 hand-picked tips that strike me as particularly clever or useful. This post is intended to be useful for beginners, but I hope even intermediate JavaScript developers will find something new in this list.
While many of these tricks are handy in any context, a few of them may be better suited for code golf than production-level code, where clarity is often more important than concision; I’ll let you be the judge of that!
So, in no particular order, here are 11 neat ways to write more concise and more performant code.
1. Filter Unique Values
ARRAYS
The Set
object type was introduced in ES6, and along with ...
, the ‘spread’ operator, we can use it to create a new array with only the unique values.
const array = [1, 1, 2, 3, 5, 5, 1]
const uniqueArray = [...new Set(array)];