Difference between JavaScript and TypeScript

JavaScript vs TypeScript

 
JavaScript is the most popular and used programming language in Web development. TypeScript is a superset of JavaScript and is getting popularity when building Web applications. While TypeScript is a superset of JavaScript, it brings many benefits for software developers that can help them build Web apps faster and in more organized way. In this blog, let's see the difference between TypeScript and JavaScript and compare JavaScript and TypeScript features.
 

JavaScript

 
JavaScript is the most popular programming language for Web development. JavaScript also known as JS is a high-level interpreted scripting programming language that is used in Web development. Unlike other popular programming languages that use a compiler to compile the language, JavaScript uses an interpreter to convert JavaScript to machine code.
 
JavaScript’s proposal was designed by Brendan Eich in 10 days in May 1995. The first official name of JavaScript was LiveScript and was shipped in Netscape Navigator 2.0. JavaScript originally managed by Netscape is currently managed by Mozilla Foundation. In 1997, JavaScript was accepted by ECMA International and became a standard. The current version of JavaScript is ECMAScript 2019.
 

Characteristics

 
Here are the key characteristics of JavaScript: 
  1. JavaScript is a high-level scripting programming language.
  2. JavaScript is a multi-paradigm programming language and supports event-driven, functional, and imperative programming styles. Structured programming syntax in JavaScript is similar to C language and supports common statements including if statement, while loops, switch statements and do while loop.
  3. While JavaScript majorly runs on the client-side in the user’s browser, but it can run on the server-side and today there are several JavaScript-based libraries and frameworks are available.
  4. JavaScript combined with HTML and CSS makes a majority of the Web pages on the Web. While HTML and CSS combo is used to build Web pages (user interfaces), JavaScript is used for functional coding.
  5. Majority of the Web browsers including Google Chrome, Microsoft Edge, Apple Safari, and Mozilla Firefox fully support JavaScript.
  6. JavaScript is a dynamically typed language that means the type of an object or variable is decided at runtime and the same variable can be used to store different types and values.
  7. While JavaScript is not a fully object-oriented programming language but is almost objects-oriented. JavaScript has concepts of objects, properties, and functions. Inheritance in objects is supported via prototypes.
  8. Function is a first-class member of JavaScript. That means, a function is considered to be an object and can have its own properties and methods.

JavaScript Libraries and Framework

 
JavaScript has become very popular among open source developers to create JS based libraries and frameworks. Some of the JS frameworks are being used to build world-class scalable enterprise applications and apps. The popular JS-based frameworks included Angular, React, Node.js, Vue.js, and Backbone.
 

TypeScript

 
TypeScript is a strongly typed superset of JavaScript. TypeScript adds static types to JavaScript. TypeScript makes JavaScript a complete programming language and adds strongly type declarative structure to JavaScript programming. TypeScript uses JavaScript and C# syntaxes and complies with native JavaScript. Today, TypeScript is being used to build large open-source projects such as Angular.
 
TypeScript was developed by Microsoft for the developers who want to build scalable applications in JavaScript and bring their knowledge and experience of structured and modern programming languages such as C# and C++. A C# developer can use classes, types, objects, their properties and methods and without too much worry about JavaScript syntaxes.
 
TypeScript uses a transcompiler or transpiler, a source-to-source compiler that translates one language into another language. TypeScript transpiler converts TypeScript code into JavaScript code.
 

TypeScript Characteristics

 
Here are the key features of TypeScript. 
  1. TypeScript developed Microsoft, is an open-source programming language that adds types to JavaScript. TypeScript runs on any platform, any device, and in any browser. TypeScript source code and project is available on Github.
  2. TypeScript is a static type language. Means, you must declare the type of variables and objects prior to using them.
  3. TypeScript adds optional types, classes, and modules to JavaScript.
  4. TypeScript supports tools for large-scale JavaScript applications for any browser, for any host, on any OS.
  5. TypeScript is compiled to clean, readable, standards-based JavaScript.
  6. TypeScript extends JavaScript syntax, so any existing JavaScript programs work with TypeScript without any changes.
  7. TypeScript is designed for the development of large applications and when compiled it produces JavaScript to ensure compatibility.
  8. The command-line TypeScript compiler can be installed as a Node.js package.
  9. TypeScript is supported by most of the popular developer IDEs including Visual Studio, Visual Studio Code, Sublime Text, Atom, Eclipse, Emacs, WebStorm, and Vim.
 
To learn more about TypeScript, visit What is TypeScript.
 

JavaScript and TypeScript Differences 

 
The following table brefily describes the differences between JavaScript and TypeScript.
 
JavaScript
TypeScript
JavaScript is a scripting language. The code is interpreted using an Interpreter.
It is an object-oriented programming language and has a compiler. The code is compiled.
JavaScript supports dynamic typing. Types can store any type of data. For example, var num.
TypeScript supports static typing. The type of the variable must be declared before use. For example, var num: number.
JavaScript doesn't support interfaces.
TypeScript supports interfaces.
JavaScript does not support optional parameters.
TypeScript supports optional parameters.
JavaScript does not support REST parameters. 
JavaScript does not support generics.
TypeScript supports REST parameters.
TypeScript supports generics.
JavaScript does not support modules.
TypeScript supports modules.
 
 
Example code for TypeScript & JavaScript is given below.
 
Code Examples
  1. class Greeter{greeting:string;constructor(message:string){this.greeting=message;}  
  2. greet(){return"Hello, "+this.greeting;}}  
  3. JavaScript:var Greeter=(function(){function Greeter(message){this.greeting=message;}  
  4. Greeter.prototype.greet=function(){return"Hello, "+this.greeting;};return Greeter;})();   

Why Choose TypeScript over JavaScript

 
Today, software development all about speed, performance, productivity, fewer bugs, and faster testing and quick releases.
Here are some of the key benefits of TypeScript. 
  • TypeScript enhances productivity with the help of Visual Studio and VS Code IDEs, debugging and unit testing TypeScript projects is much faster. Features such as Intellisense, CodeLens, and Code Share make it easier for teams to do code reviews and produce fewer bugs in the code.
  • TypeScript code is easier to understand. It supports modern programming language and simplifies JavaScript development.
  • TypeScript is less vulnerable to dynamic errors. TypeScript compiler checks objects and their types at compile time.
  • JavaScript is not for every developer. Developers coming from known Web and scripting background were not too fond of JavaScript. There were developers who have been writing code in VC++, VB, and C# for years. For example, a Windows developer who is used to developing applications for years using C++ and C# may not find JavaScript a friendly language.
  • TypeScript gives non Web developers a head start to use their existing skills and build Web applications using C# and JavaScript. The final output of TypeScript is pure JavaScript.
  • TypeScript provides classes, types, modules, inheritance, properties, and methods that are known to object-oriented programmers.
  • TypeScript is modern. TypeScript supports several modern types and functions such as functions with optional parameters, REST, Generics, and modules.
  • TypeScript helps organizes large projects into modules and helps developers create large scale JavaScript-based applications.
  • TypeScript was developed by Andres Hejlsberg, the creator of Turbo Pascal, Delphi, and C# at Microsoft and still being managed by Microsoft with the help of a strong open source community. Andres is still actively involved in TypeScript.
  • TypeScript is continuously evolving. Similar to C#, TypeScript team continuously making improvements in the language by bringing new modern and advanced features to the language.
  • Strong community. TypeScript has a strong community and its growing. TypeScript is being adopted by thousands of large open-source projects and corporations. Partnerships. TypeScript and Microsoft team has done a great job partnering with large open-source projects such as Angular to make TypeScript as the main language to build the product. 

Summary

 
While JavaScript is the mother of Web programming and used in most of the websites, TypeScript is a modern and object-oriented programming language that provides a better structure to manage large Web projects. TypeScript is being adopted by several large open-source projects and libraries such as Angular.