Difference between typescript and javascript

Samuel Tosin
2 min readApr 12, 2021

Typescript changes one of the fundamental concepts of javascript, it makes javascript which is a dynamically typed language into a language that is statically typed language or static language

what are statically typed language and dynamically typed programming language?

statically typed language

A language is statically-typed if the type of a variable is known at compile-time instead of at run-time. Common examples of statically-typed languages include Java, C, C++, FORTRAN, Pascal and Scala.

In Statically typed languages, once a variable has been declared with a type, it cannot ever be assigned to some other variable of different type and doing so will raise a type error at compile-time.

int a;
a = 100

Example above, shows we have to tell the variable what ‘a’ is that is we have to declare the variable explicitly before using them.

Dynamically typed languages

A language is dynamically-typed if the type of a variable is checked during run-time. Common examples of dynamically-typed languages includes JavaScript, Objective-C, PHP, Python, Ruby, Lisp, and Tcl.

In Dynamically typed languages, variables are bound to objects at run-time by means of assignment statements, and it is possible to bind the same variables to objects of different types during the execution of the program.

var a = 100;

Example above shows we don’t need to tell the variable what it is, it can be a string, number, boolean or etc.

Dynamic typing allows us to be more flexible and write software faster but there are some disadvantages

  1. it is not self documenting.
  2. when enough tests are not written, the codes are prone to errors and bugs.

Typescript allows Javascript to behave like statically typed language. Before Typescript there were tools like flow, reason ML. Typescript is a superset of Javascript i.e it adds functionality on top.

In order to work with typescript, you have to make sure nodejs is installed in your terminal. To check if node is installed:

node -v

what do we need node to work with typescript?

Typescript has a little engine that compiles code to Javascript which is called compiler, the compiler has to have an environment and that is what we use node in Typescript for.

Install Typescript on your command line with

$ npm install -g typescript

Create a typescript file in your computer or using the command line with

$ mkdir type
$ cd type
$ touch typescript.ts

In the typescript.ts in your text editor

In the command line in the typescript.ts file directory run;

$ tsc typescript.ts

We get a Javascript file created just like Babel compiler, it compiles the typescript code to common JS syntax.

--

--

Samuel Tosin

Fullstack Developer, Javascript Developer (PERN, MERN).