I’ll try and skip over the obvious ones to show some tips I’ve discovered.
Drop verbose literals
You can convert
Many zeros bad
If you have a number such as
3000 (possibly used with
setInterval()), you can save a character by using exponential notation, e.g.
If you need to drop the decimal portion of a floating point number (like
Math.floor()), you can use
~~ (double flip bits). Be warned, however, that this doesn’t floor for negative numbers, and it also has the side effect of truncating the
Number to 32 bits. You can also use
| 0 (bitwise OR with
If it makes sense to drop the semi-colon, then do it. This is not as often as you might think, once you’ve removed
indexOf() method, add a
~, which will make the expression truthy or falsey depending if the element was found.
There are many other methods at reducing your code on a character level, so get experimenting.
Want to discuss this post? Just mention me @alexdickson.