Sitemap
Rob O'Gara

Rob O'Gara

TypeScript versus JavaScript

2 min readMay 28, 2019

JavaScript is the most popular programming language in the world, however popular doesn’t always mean best. Every language has pros and cons, and JavaScript is no exception.

One common complaint regarding JavaScript is that it is a weakly typed language, meaning that variables can be reassigned to different data types without explicitly stating to do so, which can cause unnoticed bugs that aren’t caught until much later on in the development process. TypeScript is a great alternative that essentially turns JavaScript into a strongly typed language.

Get Rob O'Gara’s stories in your inbox

Join Medium for free to get updates from this writer.

For example,

let x = 1x = ‘powerful’TSError: ⨯ Unable to compile TypeScript:index.ts:2:1 — error TS2322: Type ‘“powerful”’ is not assignable to type ‘number’.

Another cool feature of typescript is letting functions and arrays require specific types as arguments.

But even if you want a mix of strong and weak types TypeScript allows you to set variables to :any, which means you can reassign variables to a different type.

let x: any = 1x = ‘powerful’=> powerful

The concern arises of how different TypeScript is to JavaScript and how much new code is required to learn to become proficient with it. Fortunately, TypeScript is a superset of JavaScript meaning, that any and all JavaScript carries over to TypeScript, there’s just some extra functionality that provides you with more error messages and strong data types.

Moreover, TypeScript can translate newer JavaScript syntax into old JavaScript which can come in handy when you’re writing code for old browsers that don’t support es5 or es6.

--

--