20 JavaScript Snippets to Code Like a Pro
Level up your JavaScript skills
I’ve collected a list of JavaScript shorthand snippets I love to use and I think you should know how to use them too.
I hope you enjoy it.
1. One-Liner If-Else Statements
This is a common feature in many programming languages. Instead of writing an if-else on multiple lines, you can use the ternary operator to write the whole statement with one line of code.
For example:
However, do not overuse this. It can quickly make your code more verbose too. It is wise to replace only simple expressions with this to improve readability and reduce lines of code.
2. Remove Duplicates From an Array
In JavaScript, Set is a collection that lets you store only unique values. This means any duplicated values are removed.
So, to remove duplicates from an array, you can convert it to a set, and then back to an array.