What's the difference between JavaScript and TypeScript?

JavaScript TypeScript
JavaScript is an object-based scripting language. TypeScript is an object-oriented compile language. Since it is a superset of JavaScript, its code compiles to JavaScript.
JavaScript was developed at Netscape in 1995. TypeScript was developed by Microsoft in 2012.
JavaScript has an open standard (ECMAScript) and a huge developer community. TypeScript is open source.
JavaScript is weakly typed, making it more flexible for individuals or small teams. TypeScript provides optional static typing, which can help catch errors as they are typed, therefore great for collaboration in large teams. TypeScript is also designed to scale web applications.
svg viewer

Advantages of using TypeScript over JavaScript

  • TypeScript can point out compilation errors at the time of development, which JavaScript can not do because it is an interpreted language
  • TypeScript, as mentioned above, offers static typing which allows catching type errors at compile time and therefore gives a less painful development experience

Perhaps the biggest feature that TypeScript provides over JavaScript is Type-Checking, which can prevent bugs such as the one below:

// Plain JavaScript
function fetchUserID(isUser) {
if (isUser) {
return '94321';
}
return 'username not present';
}
let userId = fetchUserID('true'); // '94321'

This code will run with invalid parameter type (non-boolean) without throwing any runtime errors. This can be avoided at compile time in TypeScript:

// TypeScript
function fetchUserID(isUser: boolean) : string {
if (isUser) {
return '94321';
}
return 'username not present';
}
// throws: error TS2345: Argument of type '"true"'
// is not assignable to parameter of type 'boolean'.
let userId = fetchUserID('true');

Disadvantages of using TypeScript over JavaScript

  • It takes time to compile the code which is not something plain JavaScript needs to do
Copyright ©2024 Educative, Inc. All rights reserved