Should I learn JavaScript or TypeScript?

The world is full of opinions. It can be very difficult to know what to believe. In a field as large as JavaScript, it’s inevitable that a variety of points of view are going to arise. To help you make an informed decision about learning JavaScript, I wrote this article.

What is TypeScript?

TypeScript is a language that compiles to JavaScript. It offers many of the features of JavaScript, but it offers a number of additional benefits, too. It gives us something called “Type safety” which means that it will force us to write code in a type-safe manner. TypeScript is developed by Microsoft and can be downloaded from the official website.

What can I do with TypeScript?

With TypeScript, you can use it to make sure your JavaScript stays clean, maintainable and reusable. It also lets you use modern type system features that make it easier to write more expressive and maintainable code. The best part is that you can mix and match, use TypeScript for part of your project, and still be able to use JavaScript for the rest.

TypeScript example — Person class

TypeScript features

Just like JavaScript, TypeScript is a modern programming language. It can feature features similar to those of a static type system and object-oriented programming. Here are a few features of TypeScript that you should be aware of:

Static type safety — what it is?

Type safety is a programming language feature for preventing type errors. It involves ensuring that variables are of a certain type before being used. It ensures that we are using variables of the correct type.

Type safety means that TypeScript will throw errors if we attempt to use variables or functions that we’ve not declared. This means that we’re going to end up having fewer surprises and will end up writing code that is more maintainable. The code that’s produced by TypeScript is more readable too.

As an intermediate or even advanced JavaScript programmer, you may not always be aware of the types of variables and functions that you’re using. You may accidentally use something that you didn’t intend to use. In TypeScript, we’ll be informed of this type of error.

Type guards

Type guards are similar to type safety. They allow us to easily perform type checks in the first place. They act as a way to make sure that we’re not using variables or functions that we don’t have declared. This means that we won’t get caught by surprise when using variables or functions.

For example, let’s say that we have a function that calculates a tax amount. We would like to make sure that we’re using the correct types of variables in order to correctly calculate this amount. We can check the types of our variables using a type guard.

Type inference

Type inference allows us to write less code. This is because the compiler is able to infer the type of variables. It can often infer the type of the variables if we omit the type. In the following example, we can omit the type annotation for the first variable, and the compiler will infer the type of it.

Another thing that TypeScript allows is for us to omit type annotations for functions as long as we provide an argument with the type that is being used for the function.

Should you learn TypeScript?

TypeScript is a very nice language, and if you are planning to create a new project or add to your existing project, I recommend that you learn TypeScript. That said, I don’t see TypeScript replacing JavaScript as the base language for the majority of my development projects.

Should you learn both?

I think learning JavaScript and TypeScript is generally fine, because the more languages you learn, the more you’re able to gain a broader perspective. Learning both the features of JavaScript and TypeScript can give you a powerful advantage over other programmers who may only know JavaScript, and so have learned very little about TypeScript.

Summary

In conclusion, I think it’s important to learn JavaScript and TypeScript because each of them offers a distinct advantage. However, if you are new to programming, I recommend starting with the features of JavaScript first, and later adding the features of TypeScript if you feel that it would benefit you.

However, if you just want to develop frontend, you can learn only JavaScript.

Leave a Comment