JavaScript’s a mess – and that’s a good thing
JavaScript never removes old features – new versions are always backward compatible. I wish we could clean it up, but staying compatible has too many benefits, as I’ll explain in this blog post.
The benefits of backward compatibility #
These are benefits of staying backward compatible:
- It is trivially easy to migrate to new language versions. For example, all old code was already compatible with ES6 when it came out.
- Only ES modules are relatively difficult to adopt – but they kind of do break backward compatibility.
- It avoids versioned code. If you allow some code to be “old” and some code to be “new”:
- Language engines and tools become more complicated.
- Programmers need to be aware of versions.
- You can’t move code around, anymore (if a code base is mixed).
Tips for dealing with JavaScript’s expanding feature set #
- Teaching and learning: you can mostly ignore old features, apart from what they look like and – roughly – what they do.
- Let linters help you with using the language properly.
- Let Prettier help you with formatting source code properly.
A cleaner JavaScript #
If you want to program in as clean a JavaScript as possible, there is much you can ignore (some suggestions are more radical than others):
var
. Use let
and const
, instead.
function
. Use only arrows and method definitions. Benefit: handling this
becomes much simpler (details).
- Promises. Use only async functions. Learn what to watch out for (you can’t ignore Promises as completely as
var
).
- Iterating over objects. Use Maps, instead.
- Loops:
for-in
(avoid always), for
(avoid if you can). Use for-of
, instead.
arguments
. Use rest parameters (...args
), instead.
Function.prototype.apply()
. Use the spread operator (f(...myArray)
), instead.
- Constructor functions. Use classes, instead.
- IIFEs. Use blocks, instead.
Wish #
typeof
vs. instanceof
is too complicated. This blog post describes a library for simplifying things.
Further reading #