Typescript is the better Javascript

Subscribe to my newsletter and never miss my upcoming articles

Introduction

Hello there! Javascript is THE language of the web and one or maybe the most used language in the world of programming. You can do everything with it, from building a simple portfolio website till build a complete SaaS product.

When i started learning Javascript i was hooked! I loved the language because its so versatile but over the time i realized that versatility has its price. And only Typescript can pay it!

Static vs. dynamically typed languages

Javascript is a dynamically typed language that means that you dont specify a type for variables or functions. Javascript takes care of that. The type can change AFTER compile time (at the runtime).

This means you never know for sure if you assign a number to a variable if this variable really holds a number during the lifetime of the program. This can lead to really awkward errors.

For example this is valid Javascript Code:

2 + "2" = "22"

Seems weird if you ask me.

Typescript on the other hand is a static typed language where you define the type at compile time while you are coding. This means you get immediate feedback from your IDE when you try to assign a string to an integer variable for example. This reduces error and unwanted behavior to almost zero!

The second argument for static typed languages are that the programmer can read the code better and understand it more quickly. You can see which method returns which type, which parameter is which type.

Typescript

But what is Typescript? Typescript is a superset of Javascript and was developed by Microsoft. Valid Javascript code is always valid Typescript code. And with Typescript you got many new features added to Javascript. Here are some examples:

Types

The beauty of Typescript. You can give your variables, methods etc. types!

let myName: string = "Dany";
let myAge: number = 27;

public getPersonAge(): string {
    return this.myName;
}

You also can see that we can give our variables, methods etc. an access modifier like public or private to decide which is available only inside a class or also outside.

And even in Typescript you can give a variable a dynamic type if you really need to. In Typescript there is the "any" type.

Object Oriented Programming

Sure in Javascript you also can code object oriented but in Typescript ist much, much easier! You can access modifiers like mentioned above, you get classes and interfaces and much more.

I really enjoy coding object oriented and its easy and fun in Typescript because you get things like mentioned above, Inheritance and such things out of the box!

Conclusion

In my opinion Typescript is the better Javascript. You get a lot of cool, helpful features on top and no disadvantages at all.

As most things in life it depends on the person. Some of you may like and enjoy the freedom of Javascript and dont want to bound to typed and such things. And thats okay. But for me and may others out there Typescript really helps to organize the code better and enjoy to code more object oriented which is much easier with Typescript.

Stay connected to me and my content on Twitter.

I love to improve myself every single day even if its just a tiny bit!

Stay save and healthy guys!

And as always: develop yourself!

Usman Sabuwala's photo

No doubt TypeScript is superior than JavaScript. And it's one of the best thing happened in the JS ecosystem. Very nice writeup.

The only downside of TypeScript is the hassle of compiling it to JavaScript and not being able to directly use it or run it.

Dany Tulumidis's photo

thanks mate! Jeah thats true but thats one prize im happy to pay for all these nice features that Typescript offers me!

Pratik Sharma's photo

HEEEEYYY, Great article Dany Tulumidis. TypeScript is surely going to overtake JS in the long run. I recently posted a list of resources to learn TypeScript. Free Resources to learn TypeScript

Dany Tulumidis's photo

Thanks! Thanks for sharing the knowledge!

Hemant Joshi's photo

No Doubt, Typescript is better than Javascript. People blame javascript for not being oo but typescript comes and kicks every blame.