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. |
Perhaps the biggest feature that TypeScript provides over JavaScript is Type-Checking, which can prevent bugs such as the one below:
// Plain JavaScriptfunction 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:
// TypeScriptfunction 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');
Free Resources