TypeScript vs JavaScript
TypeScript
is a superset of JavaScript
, it is the ES5
version of JavaScript with some additional features.
It is a statically compiled language which is used to write JavaScript code in a clear and simple way.
You can write Angular applications either in TypeScript, ES6
or in ES5
, JavaScript.
However, Angular itself is written in TypeScript.Most of the examples on the web are written in TypeScript,
also most Angular jobs require you to write code in TypeScript so in this tutorial code will be writing in TypeScript.
- TypesScript is known as open-source programming language whereas JavaScript is a scripting language.
- TypeScript has a feature known as static typing that enables static language analysis but JavaScript does not have this feature.
- TypeScript adds support for features like modules whereas JavaScript does not support modules.
- TypeScript has Interface feature but JavaScript does not have Interface.
- TypeScript support optional parameter functions but JavaScript does not.
Advantages of using TypeScript over JavaScript
- TypeScript always points the errors at the time of development. Because of this at the run-time, the chance of getting errors are too less whereas JavaScript is an interpreted language.
- TypeScript has a feature which is strongly-typed or supports static typing which means it allows checking type correctness at compile time, where it is not available in JavaScript.
- TypeScript is nothing but JavaScript with some additional features i.e. ES6 features. It may not be supported in your target browser but the TypeScript compiler can compile the .ts files into ES3, ES4, and ES5 also.
- Typescript has the feature of prototyping but JavaScript doesn't have this feature.
- Compilation − JavaScript is an interpreted language. Hence, it needs to be run to test if the code is correct or not. Hence, you have to spend hours to find bugs in the code. Whereas the TypeScript transpiler provides the error-checking feature and it will compile the code and generate compilation errors if it finds some sort of syntax errors. This helps to correct errors before the script will run.
- Strong Static Typing − JavaScript is not strongly typed whereas TypeScript provides optional static typing and types inference system with the help of the TLS (TypeScript Language Service).
- TypeScript supports definitions files that can contain type information of existing JavaScript libraries. TypeScript Definition file (with .d.ts extension) provides the definition for external JavaScript libraries. Hence, TypeScript code can hold these libraries.
- TypeScript supports Object Oriented Programming (OOPs) concepts like classes, interfaces, inheritance, polymorphism, data abstraction.
Disadvantages of using TypeScript over JavaScript
- Generally, TypeScript takes too much time to compile the code.
- It does not support abstract classes.
- To run the application in the browser, first, it is required to transform TypeScript into JavaScript.