Practical Ways to Write Better JavaScript

Last updated 2 years ago by Ryland G


I don't see enough people talking about practical ways to improve at JavaScript. Here are some of the top methods I use to write better JS.

Use TypeScript

The number one thing you can do to improve your JS, is by not writing JS. For the uninitiated, TypeScript (TS) is a "compiled" superset of JS (anything that runs in JS runs in TS). TS adds a comprehensive optional typing system on top of the vanilla JS experience. For a long time, TS support across the ecosystem was inconsistent enough for me to feel uncomfortable recommending it. Thankfully, those days are long behind us and most frameworks support TS out of the box. Now that we're all on the same page about what TS is, let's talk about why you would want to use it.

TypeScript enforces "type safety".

Type safety describes a process where a compiler verifies that all types are being used in a "legal" way throughout a piece of code. In other words, if you create a function foo that takes a number:

function foo(someNum: number): number { return someNum + 5; }

That foo function should only ever be called with a number:


console.log(foo(2)); // prints "7"

no good

console.log(foo("two")); // invalid TS code

Aside from the overhead of adding types to your code, there are zero downsides to type-safety enforcement. The benefit on the other hand, is too large to ignore. Type safety provides an extra level of protection against common errors/bugs, which is a blessing for a lawless language like JS.

starring shia lebouf

Typescript types, make refactoring larger applications possible.

Read full Article