You wouldn’t need this JavaScript trick

If you just used TypeScript

Here’s a JavaScript trick that’s been making the rounds on the internet:

You know what has a concept of optional arguments? … Type Systems. And as type systems go, TypeScript is a pretty darn good one. Just try the following in TypeScript (go ahead … click me and see for yourself):

The difference is that you got the error at compile time before pushing or even running any code. Having such cases as an error is similar to other good existing JavaScript practices … e.g.

So in TypeScript (which is just a checked superset of JavaScript), if you say you are going to take an argument, TypeScript will tell if you call a function without one (TypeScript errors are really just super awesome linter errors). Of course you can always mark something as *optional* with a ? annotation:

Now jumping back to the original raw JavaScript trick:

As tricks go its actually a potentially disastrous one. I’d be scared to put that into an existing project and pushing it to production. However moving an existing project to TypeScript …. I’d do it in a heartbeat🌹

All that said. If you want to use that trick in raw JS, I wouldn’t judge you.