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!
Top comments (7)
Yes, TypeScript is better than JavaScript, the same way C is better than Assembly language. You move up one layer higher, away from the bare metal. There is always a strong discussion between the devs who believe this is "better" and the devs who've been using the Legacy language and don't agree.
jeah thats for sure, i really like Typescript and thats why i chose that controverse title. It depends on your needs and how you like to work and in my case Typescript just fits perfectly!
Typescript is a compile-time typed superset of javascript. I've seen the type system used to great advantage in some cases and being useless in some others. So typescript being better vastly depends on how you use it.
Pardon my ignorance but why do you have quotes around 27 for your type declared number variable myAge? Shouldn’t that throw a compiler error?
thanks, my fail :D
I’ve never used typescript in a real project but am curious. Just yesterday in Python I passed the wrong type into a function whose arguments are “typed” and there were no errors at the time the function was called, only after the function failed part way through. It appears as though typing in Python is only for assisting with the development process in giving hits, it doesn’t actually throw warnings the types declared weren’t used when the function was called. Thanks for the article!
im glasd you liked it and learned something from it. Jeah this is exactly a usecase where Typescript comes and saves you :D I really enjoy static typed languages