JavaScript and TypeScript

Typescript vs Javascript

Posted in JavaScript, TypeScript
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. 

Difference between Typescript vs Javascript

Essentially, all your JavaScript code is also valid in Typescript – that means Typescript is a superset of JavaScript – or

JavaScript + more features = Typescript.

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 –

 

TYPESCRIPT

JAVASCRIPT

Superset of JavaScript developed to overcome code complexity for large projects

A scripting language that helps create dynamic web page content

Errors can be found and corrected during compile time 

Errors can be found only during run-time as it is an interpreted language

Strongly typed, supports both static and dynamic typing

Weakly typed, no option for static typing

Converted into JavaScript code to be understandable for browsers

Can be directly used in browsers

Since it is a superset, all the JavaScript libraries, and other JavaScript code works without any changes

JS libraries work by default

There is support for ES3, ES4, ES5 and ES6

No support for compiling additional ES3, ES4, ES5 or ES6 features 

Supports modules, generics and interfaces to define data 

No support for modules, generics or interface

Functions can have optional parameters

This feature is not possible in JavaScript

An 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 language

Neat and clean, most suitable for simple web applications

The community support is still growing and not so huge

Huge community support with lots of documentation and support for solving issues

Prototyping is possible

Prototyping support is not there

Takes 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 definitions

No 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. Start learning TypeScript here.

Ramya Shankar

Ramya Shankar

A cheerful, full of life and vibrant person, I hold a lot of dreams that I want to fulfill on my own. My passion for writing started with small diary entries and travel blogs, after which I have moved on to writing well-researched technical content. I find it fascinating to blend thoughts and research and shape them into something beautiful through my writing. View all posts by the Author

Leave a comment

Your email will not be published
Cancel