Typescript vs Javascript: Learn the difference

Typescript and Javascript
Typescript and Javascript

JavaScript is a familiar name in the web developers community as it one of the most widely used client-side scripting languages. HTML, CSS and JavaScript form the front-end design of a webpage by applying code that affects the content, style and interactivity of the site. This was how the front-end of a normal website was designed during the early years. In 2012, the announcement of the public release of Microsoft’s TypeScript changed the way websites were
developed from henceforth.

The typeScript was introduced as a superset of JavaScript, i.e., it is JavaScript plus some other added features. It was developed to overcome the drawbacks of JavaScript as it tends to get
messier when the code grows, making it difficult to maintain and reuse. This raises a lot of questions for someone new to web development. What sets the two languages apart? Is one
better than the other? To determine that, we would need to look into the comparison between
them. But before that, let’s dive deep into what JavaScript and TypeScript are.

What exactly are JavaScript and TypeScript?

During the early years of Web, webpages could only be static and had no dynamic behaviour
after they were loaded from the browser. In 1995, the founder of Netscape Communications had a vision that these webpages needed a way to become more dynamic. This led to the birth
of JavaScript.

JavaScript drew inspiration from Java, Scheme and Self. Although it held a strong resemblance
in language name, syntax and standard libraries with Java, the two languages are distinct and
differ greatly in design. While HTML and CSS give structure and style to your website, JavaScript
lets you add functionality and behaviours to it. With the recent development in NodeJs, JavaScript could also execute code on servers.

Despite facing a rough beginning, JavaScript grew to become one of the most dominant clients-
side scripting languages with almost 95% of web pages using it.

On the other hand, TypeScript is an open-source language that is relatively new to the market. It was developed by Microsoft to help catch mistakes early and to make JavaScript development more efficient. It is no different from JavaScript in its purpose but it is found to be more suitable for large applications.

Through TypeScript, you can safely use new features such as modules, lambda functions, classes, while remaining backwards compatible with older browsers and JavaScript runtimes. It
can be used to develop JavaScript applications for both client-side and server-side execution.
Now that you know why and how these two languages were developed, here are some of the
key differences between them.

The Type System that makes them divergent

The main purpose of a type system is to reduce bugs in programs and this checking can happen either statically or dynamically. Oftentimes, Type systems are specified as part of programming languages and built into interpreters and compilers.

TypeScript has the feature of static typing and checks for all variables and expressions against their types are performed during compilation time. It provides greater performance and offers more opportunities for compiler optimizations. Static typing also reduces the likelihood of some kinds of errors.

JavaScript doesn’t support this feature as it is dynamically typed. It can lead to a problem where operations with incompatible types can produce unpredictable results in our software. But on the bright side, dynamic typing is flexible.

Do they follow the same programming paradigm?

JavaScript is regarded as a scripting language that translates the source code into machine code when it is run on the browser rather than beforehand. This gives an edge over TypeScript as scripting languages are easy to learn even for beginners with no prior experience.

The typeScript was introduced to the public as an Object-Oriented programming language and supports OOP concepts like abstraction, classes, interfaces, inheritance, generics and plenty more with its smaller and clearer syntax. It improves code quality and maintainability and provides code reusability.

Compilation or interpretation?

JavaScript is an interpreted language. It has no compilation step and involves an interpreter that reads over the JavaScript code, interprets each line and runs it. This is the reason it runs directly on the browser. Unfortunately, this means errors can be found only during run-time, leading to decreased execution speed.

Being a compiled language, TypeScript is compiled into its JavaScript equivalent before being used on the browser. It highlights errors during the development time and helps catch them in the early stage. It tends to be faster than interpreted languages as the process of converting code at a run time adds to the overhead.

Does it support Prototyping?

A prototype is a design pattern that lets you clone objects and add methods and properties to
them. Prototyping is possible in TypeScript whereas JavaScript doesn’t support this feature.

The syntax of TypeScript and JavaScript

Syntax in programming language refers to the set of rules for writing a program. A Typescript
programme consists of functions, modules, statements, variables, and comments. A TypeScript file is saved with the extension .ts or .tsx. In JavaScript, all statements are written inside of a script tag. The browser program interprets and executes all the text is within these tags like a script. JavaScript files are saved with the extension .js.

How about Annotations?

Annotations are structural elements that contain meta-information in the source code of the
programme. It can be used for different purposes like documentation, hints, error avoidance and
also for compiler control. While using TypeScript, it is strongly suggested that the code should be annotated persistently to get the most out of TypeScript Features. Annotations are usually not required in JavaScript.

Which one is better based on performance, TypeScript or JavaScript?

Grading TypeScript and JavaScript in terms of performance is subjective as both languages are built with their benefits and limitations. Some of the performance differences that are worth noting are:

TypeScript:

  • Takes time to compile the code.
  • Easy to debug and early detection of errors is possible
  • Allows better development time tool support

JavaScript:

  • Faster when it comes to coding for application
  • Developers will not need to wait for the page to reload as it references the code immediately
  • A single error in a JavaScript file can stop the rendering of the entire code on the website

Even with all the added functionalities and benefits TypeScript offers, it can not replace JavaScript in the web development industry. JavaScript remains as one of the most preferred client-side scripting languages up to this date. When you are working on a small scale project with limited man-power, JavaScript is a great option for you.

If your team is equipped with expertise and can handle complex projects, going for TypeScript is a perfect choice. TypeScript developers are paid an average salary of $148,000 per year whereas JS developers are paid around $110,000 per year. By the end of the day, whether you choose JavaScript or TypeScript for the client-side scripting is completely based on your requirements.

Liked this article? Read more about Javascript here.