The differences between Javascript and Typescript

This article was originally published on .cult by Piumi Gunawardhana. .cult is a Berlin-based community platform for developers. We write about all things career-related, make original documentaries and share heaps of other untold developer stories from around the world.

In the beginning, there was JavaScript. The popular scripting language we all know and love. Ever since its creation, it’s been the dominating language for web development. Then two decades later, Microsoft introduced TypeScript, a superset of JavaScript, designed for the development of large web applications.

If you know anything about Typescript and JavaScript, you’ll know they’re not competing forces. They have a unique relationship, working hand in hand. That being said, there are some differences — and that’s what I want to discuss in this article. Let’s break down the pros and cons so you can better decide which scripting language is right for your next web application.

What is JavaScript?

JavaScript is the most popular programming language in the world.

Let’s start at the beginning. JavaScript has been the core technology for web applications since the 90s. It’s used to create interactive web pages in combination with HTML and CSS. It’s a high-level language with dynamic typing and a JIT (Just-in-Time) compiler.

JavaScript is a multi-paradigm language that supports various programming styles like functional programming, imperative programming styles, and event-driven programming. It also supports server-side implementations. JavaScript is formed on the basis of the ECMAScript (ES) standard. The most widely used ES standards with JavaScript are ES5 and ES6.

What is TypeScript?

TypeScript (TS) is a JavaScript superset that serves the same goal as JavaScript.

TypeScript is an open-source programming language rich in many features like inheritance, classes, visibility scopes, namespaces, interfaces, unions, and other modern features along with static and dynamic typing. It allows comments, variables, functions, statements, modules, and expressions.

Being a strongly typed programming language, TypeScript allows easier debugging (during the compilation), which is a more effective approach to codingfor complex applications.

Why TypeScript Emerged?

The complexity of JavaScript projects tends to grow exponentially. Initially, JavaScript was only used as a client-side language. But developers realised that it could also be used as a server-side programming language.

Despite the advantages of JavaScript, it can get messy and complex on the server-side (especially with larger-scale apps). In addition, JavaScript has also made it hard to maintain larger complex applications.

Browser improvements and cross-browser compatibility also demand changes for the underlying JavaScript but writing future-proof JavaScript isn’t practical. As a result, TypeScript emerged to meet these application needs.

Key Differences between JavaScript and TypeScript

Javascript Typescript
Supports dynamic web page content

The language is interpreted, therefore errors are only discovered during runtime

Weakly typed, no choice for static typing

Can be used directly in browsers

Modules, generics, and interfaces are not supported

Does not support optional parameters

Numbers and Strings are used as interfaces

Massive community support, including extensive documentation

There is no support for prototyping

No prior scripting knowledge is required

No need to set up a build environment

JavaScript superset created to help the project with code complexity

During compilation errors can be detected and fixed

Strongly typed; both static and dynamic typing are supported

Converts code to JS for browser compatibility

Supports modules, generics, and interfaces

Optional parameters can be added to functions

For static type definitions, a proper build setup (npm package) is necessary

Numbers and strings are objects

Community support is growing, not as strong as it once was

Prototyping is a viable option

Learning and coding takes time, scripted knowledge is required

Is TypeScript better than JavaScript?

The way I’ve described it, it might seem like TypeScript is just a better version of JS. So you might think it’s likely TS will replace JavaScript in near future… well not really. I still believe JavaScript will have its place.

Complexity is a key factor to consider.

JavaScript is ideal for simpler applications because it operates on all platforms (cross-platform) and is very lightweight. Additionally, compiling TypeScript code requires time and CPU resources compared to a minimal overhead with JS. Sometimes the hassle of using TypeScript doesn’t make sense for the project.

However, TypeScript comes with many benefits over JavaScript.

TS makes code refactoring much easier and also emphasizes more on explicit types allowing the developer to grasp how various components interact. As it supports compile-time debugging, there’s a definite benefit to organizations working with large complex applications.

Setting up TypeScript for any project is straightforward. Some frameworks like Angular use TypeScript by default. So, in my opinion, TypeScript is the clear winner here.

Disadvantages of TypeScript

Despite the many advantages that TypeScript provides, it also has certain disadvantages.

Defining types explicitly requires more work initially but saves both time and resources in the long term. Despite this, it is the most commonly used point in support of JavaScript.

You can’t just migrate a large JavaScript project to rigorous idiomatic TypeScript on the fly. Although there are some tools to assist, you’ll still have to do most parts on your own.

TypeScript code compilation takes time and CPU resources. There’s also a waiting period for the code changes to appear in the browser. Unlike conventional JavaScript, real-time coding affects a little. On massive codebases, the tools also slow down significantly, causing significant latency in navigation, etc.

When should you migrate your project to TypeScript?

When the code grows in size, complexity, and error proneness, it is preferable to identify specific problems during the compilation process. That’s where TypeScript comes in handy.

TypeScript also has interfaces and access modifiers that allow developers to collaborate and interact on a single codebase. Therefore, it’s probably best to use TypeScript from the beginning of your project.

However, if you prefer frameworks like Ember.js or Glimmer.js you’ll have limited support for TypeScript. So JavaScript would be the language of choice for these frameworks.

Conclusion

Since TypeScript converts to ordinary JavaScript code, you could use it as an alternative to JavaScript. This is why it’s gained in popularity. We’ve also seen TS integrated into the core building components and libraries of the popular JS frameworks like Angular, React.js, and Vue.js.

Personally, I’m a fan of TypeScript and generally recommend using it for new projects. A little more effort at the beginning to save a headache down the road makes a lot of sense to me.

I hope that this article has helped you get a better understanding of the differences between these two scripting languages. At the very least, you’ve got a better understanding of what exactly TypeScript is (if you didn’t already know). Now you can make a more informed choice for your next project 😉

Source link

Latest

Amazon announces London ‘micromobility’ hub and ditches vans for e-bikes

The delivery giant said its new ‘micromobility’ hub in London will lead to a million more customer deliveries each year, while others are expected...

Army’s YouTube and Twitter accounts hacked

The British Army says it is investigating after videos and posts for cryptocurrency appeared on its accounts. Source link