Typescript vs Javascript

If you have ever worked on a web development project, you must have seen what JavaScript is like. JavaScript has been there for a long time now as the most popular scripting language for many web projects.

Typescript is an open-source programming language most suited for large applications. It was developed by Microsoft in 2012, mainly because JavaScript code was becoming too complex to handle when it comes to large-scale applications.

Table of Contents

  • Difference between Typescript and Javascript
  • JavaScript
  • TypeScript
  • Does that make TypeScript better than JavaScript?
  • When should you migrate your project to TypeScript?
  • Typescript vs Javascript: Head to head comparison
  • Conclusion

So, if you save your JavaScript (.js) file with a Typescript (.ts) extension, it will work perfectly fine. But that does not mean that Typescript and JavaScript are the same.

Before outlining the differences between both, let us understand what each language looks like!

JavaScript

JavaScript is one of the most popular core technologies of the web. From the beginning, it has been an integral part of web applications making web pages interactive and dynamic. It is a high-level language, with JIT (Just-in-Time) compiler and dynamic typing. For long, JS was a client-side implementation, but some newer JS engines also have server-side implementations. The syntax of JS is very similar to Java, and so are the standard libraries. As a starting point, JS is the best scripting language to learn. Check some nice tutorials here.

JavaScript was developed by Netscape in collaboration with Sun Microsystems.

Some unique features of JavaScript are –

  • Flexible, dynamic and cross-platform
  • used for both client-side and server-side
  • Lightweight interpreted
  • Supported by all browsers
  • Weakly typed
  • JIT compilation

Let us take a simple example to illustrate how JavaScript works. The following simple HTML code is to validate a username field where myFunction() is written in JavaScript.

If you observe, the syntax of the functions is similar to Java, however, we have defined the variables as ‘var’ and not declared them as any type. The myFunction() function is triggered when the user clicks on a ‘Submit’ button and gives appropriate alert messages as per the conditions. Here is sample output –

It is that simple! If you know Java, JavaScript is fairly simple to learn.

TypeScript

TypeScript is no different from JavaScript in its purpose but is used for developing large applications. TypeScript trans compiles (source to source compilation) to JavaScript. It follows an object-oriented programming language structure and supports its features like classes, interfaces, namespaces, and inheritance. Static typing is possible in TypeScript through type annotations (numbers, string and boolean). For example,

class Student {
private name: string;
}

As we see above, TypeScript is strongly typed. This makes it better to debug (during compile time itself) which is a more efficient way to code for large projects. TypeScript program typically consists of modules, functions, variables, comments, expressions, and statements — just like any other full-fledged programming language. Some prominent features of TypeScript are –

  • Easy to maintain and enhances project productivity
  • Static typing and annotations are possible
  • Supports object-oriented features like interface, inheritance, and classes
  • Easy to debug and early detection of errors
  • Supports ES6 (ECMAScript) that offers easier syntax to handle objects and inheritance features
  • The good full-fledged IDE support

Does that make TypeScript better than JavaScript?

Before further comparison of TypeScript and JavaScript, another important question needs to be addressed! Since TypeScript is a superset of JavaScript, should we always use Typescript? Does being a superset makes TypeScript suitable for all types of projects?

No.

TypeScript is no way to replace or make JavaScript obsolete. JavaScript is still the most favorite client-side scripting language. For smaller projects, using TypeScript could be an overhead because it takes time to trans compile the code into JavaScript, which is an extra step.

JavaScript is directly run on the browser, so for small code chunks, it’s easier to refresh and debug the code. In the case of TypeScript, we need a proper IDE and set up to run the code.

When should you migrate your project to TypeScript?

When the code becomes huge, complex to handle, and more prone to errors, it is better if some errors are caught during compile time itself. That is where TypeScript helps. The beauty is that the entire codebase written in Java can be reused as such.

Typescript vs Javascript: Head to head comparison

Now as we understand the basic features and purpose of both, let us explore some more low-level differences apart from what we have already learned so far –

TYPESCRIPTJAVASCRIPTSuperset of JavaScript developed to overcome code complexity for large projectsA scripting language that helps create dynamic web page contentErrors can be found and corrected during compile timeErrors can be found only during run-time as it is an interpreted languageStrongly typed, supports both static and dynamic typingWeakly typed, no option for static typingConverted into JavaScript code to be understandable for browsersCan be directly used in browsersSince it is a superset, all the JavaScript libraries, and other JavaScript code works without any changesJS libraries work by defaultThere is support for ES3, ES4, ES5 and ES6No support for compiling additional ES3, ES4, ES5 or ES6 featuresSupports modules, generics and interfaces to define dataNo support for modules, generics or interfaceFunctions can have optional parametersThis feature is not possible in JavaScriptAn example code –

function multiply (a, b) {
return a*b;
}
var result = multiply(a, b);
console.log(‘The answer is - ’ + result);

Example code –

<script>
function multiply (a, b) {
return a*b;
}
var result = multiply(a, b);
document.write (‘The answer is – ’ + result);
</script>

Numbers, Strings are considered as interfaces.Number, string are objects.Powerful and intuitive languageNeat and clean, most suitable for simple web applicationsThe community support is still growing and not so hugeHuge community support with lots of documentation and support for solving issuesPrototyping is possiblePrototyping support is not thereTakes time to learn and code, scripting knowledge is a must.Can be learned on the go, no prior scripting experience is needed.Proper build setup (npm package) is required for static type definitionsNo build setup is required

Conclusion

As we have already determined, JavaScript is most suited when your team is new and is working on small web projects. If you have a team with good expertise and knowledge and want them to handle a complex project, going for TypeScript is a perfect choice. That said, if you are looking for a learning curve and job opportunities, definitely TypeScript has an edge over JavaScript. TypeScript developers are paid an average salary of $148,000 per year whereas JS developers are typically paid around $110,000 per year. If you learn TypeScript, you can work on both JS as well as TypeScript projects.

Dot Net Developer. SQL Server, Visual Studio, IIS, Azure.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store