JavaScript or Typescript or both đ¤ˇââď¸?
As developers, we sometimes get stuck deciding which frameworks/language to use, especially if we know more than one that suits our needs or want to learn. Oftentimes, learning more about what makes them unique points you in the right direction.
This blog covers what JavaScript and Typescript are, their uses, differences, and interoperability.
What is JavaScript?
According to MDN,
âJavaScript is a scripting or programming language that allows you to implement complex features on web pagesâ. â MDN
In my own words, JS is a programming language that gives behavior to your webpage, that is, it provides the ability for your webpage to interact with and react to changes made by the user. Simply put, itâs the main difference between a static site and a dynamic site.
What is Typescript
According to the official documentation,
âTypeScript is an open-source language which builds on JavaScript⌠by adding static type definitions.â â Typescript docs
Well said, but whatâs static type definitions? Simply put, static typing is the art of declaring a variable before using it so that you canât use variables you havenât explicitly declared. That way, some developer errors (e.g. when you make a spelling mistake) will throw a syntax error instead of a semantics error (i.e itâll throw an error during compile time instead of during run time) learn more here
How are they used/what are they used for?
Simply put, JavaScript is used for making webpages react to user interaction. Typescript gets compiled into JS to do the same thing JS does.
Are they interchangeable?
Since Typescript gets compiled to JS, it would be safe to say they are interchangeable, which in this context means you can use Typescript instead of JS or vice versa. TS is compatible with Babel, swc, and Sucrase but youâll still need the typescript dependency (and you might need to enable isolatedModules).
Which should I use?
You probably guessed I wonât give a direct answer here đ, itâs simple though. When deciding between JS and Typescript, you should take into consideration the size of the project, the current status (if its an ongoing project or just needs maintenance or youâre just starting out), the company culture/your personal preference, and your teamâs opinions (especially if youâre thinking of switching from JS to TS), amidst other factors. Some people prefer plain old JS while some love the type-checking and early on error-catching that Typescript provides.
Getting started with typescript
Install Typescript in Node via
npm install typescript --save-dev
//or
yarn add typescript --dev
You can also install it as an extension on VSCode
Learn more about installing and using TS
Can I use both?
Yes! According to the official typescript documentation,
âAdopting TypeScript is not a binary choice, you can start by annotating existing JavaScript with JSDoc, then switch a few files to be checked by TypeScript and over time prepare your codebase to convert completely.â â Typescript documentation
While I donât expect every reader to completely understand what the above quote means, you should know that since TS results in JS, they can be used together, with some modifications. I found this well detailed stack overflow post about using both.
Personal thoughts: Even though TS is a language in its own right, I prefer to think of it as some add-on library like Redux. It might take some time to learn and set up, but after that, developer life gets +1 easier.
Thatâs all for now. If youâre still unclear about something, let me know in the comments below or look it up on gđgle.
Thanks for reading!