You Should Never Write Plain JavaScript Code Again

Sina Farhadi
JavaScript in Plain English
3 min readNov 23, 2022

--

Maybe the title looks a bit clickbait, but I believe that everyone should not write dynamically typed JavaScript code after this article!

Photo by Gabriel on Unsplash

Why not plain JavaScript?!

One of the caveats or maybe benefits of JavaScript is that being a dynamically typed language, it means you don’t have to put the strict types of variables or void returned value in front of them, Looks easy and straightforward but please no! if you’re just one person and work on your small 20 lines of javascript code then you can ignore static typing but please don’t write your codes without static types anymore!

We can ignore the last years of not using TypeScript because of the lack of the libraries and frameworks support and coverage for it, but now hopefully the coverage is way too much enough.

Why Static Typed?

I believe that the biggest reason is the endowment of the Predictability and Readability of your code, your code before being statically typed can be anything! absolutely anything. imagine you have a variable with the name of “user”, then think of the possible options of what this variable can be, it can be a string, number, object, Promise, or any other thing!

let user;
// string?
// number?
// object?
// function?

Why this is important? When you’re coding and know that your code will be used and read by others, even for your self in 1 year, then you should be in the shoes of that person and think about that how they see the code. how are they gonna contribute to it? can they understand the purpose? if this question doesn’t make sense, then you can skip this article.

Now let’s look at the statically typed version of the code below;

let user: string;
// Yeah! it will be string.

What are the options?

We don’t have many options to statically type JavaScript, actually, the only best one commonly used is TypeScript.

We have other options like Flow or maybe some IDE extensions but they are not even a competitor to TypeScript.

TypeScript

TypeScript is a superset of JavaScript that is a strongly ( statically ) typed language, it means that your code is in the syntax of TypeScript but finally you’ll get a JavaScript code for the output.

The syntax may look a bit crazy at the first and it’s totally normal for a person that always coded in a dynamically typed language, so don’t be scared: )

Let's see some examples of this gift!

let user: string;

function phone_verifier(phone: number): bool {
return true;
};

This is fantastic! you know that what you wrote, what is the purpose and others will do too.

Thanks for reading this article, if you mind seeing more kinds of this, then you can take a quick look at my Medium page.

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.

--

--

Senior Frontend Developer | Expert in React.js, Next.js, and Svelte // Looking for new opportunities 🏄‍♂️ https://bugpointer.dev