Let’s find out… From the time you started coding, JavaScript (JS) would have been part of all your front-end projects. If you are a little familiar with JS, think of TypeScript as JS plus some additional features that make your application neater and typed. The typeScript was developed as an open-source project by Microsoft to make JS development more efficient and catch compilation errors early on. In this article, we will discuss some important features of JavaScript and TypeScript and the differences between both scripting languages.

What is JavaScript?

JavaScript is used for client-side scripting. You can create a script on an HTML page or create a file with a .js extension and include it in your HTML file. A common real-world example where you can see JavaScript is the login page validation, where you are shown an error when your username/password is incorrect. Let us write a simple JS code and run it on the browser: Create a file example.html and add the following code: This simple code declares a variable and assigns a value of happy (string) to it. We can assign a value of a different type (number) to the same variable. JavaScript wouldn’t complain of it and we can run the code on any browser without any issues. Now, let us get the value of feeling from the user: Our HTML will look like this: and the script will be: Unless we put explicit checks in the script, JS will accept any value from the user and pass it on. So, you can put anything like 234, @.#$%, etc. in the feeling variable.

Features of JavaScript

From the above, we can observe the following features of JavaScript:

Weakly typed scripting language Used for client-side and server-side (with node.js) scripting Flexible and dynamic Supported by all major browsers Lightweight and interpreted

If you are interested in mastering JavaScript then check out this Udemy course.

What is TypeScript?

A real-world application will have many validations and dynamic checks. For such applications, JavaScript code will become hard to test at some point, mainly because there is no type checking. While getting values from users, it is important to get them right at the beginning itself.  This is where TypeScript pitches in. TypeScript is strongly typed and has a compiler that complains if you don’t define the type of a variable. Both JavaScript and TypeScript conform to the ECMAScript specifications for a scripting language. Typescript can run all the JavaScript code and supports all its libraries – that’s why it is called the superset of JavaScript.

TypeScript installation

To execute our previous code in TypeScript, we need to install the TypeScript compiler using the npm package (if you have node js). or download it directly from the official Microsoft downloads page. You can also use the TS playground to see how the code is trans-compiled from ts to js. Once this is done, you can configure the project settings in tsconfig.json and use any IDE or text editor to write TypeScript code and save it as .ts. You can still get away by not defining the variable type and TypeScript can infer the data type. However, you will get a ‘feeling’ error if you give anything other than the first used type (in our case, a String) – during compilation itself. It is always neat for the code to have a type annotation for maintainability and ease of use: That’s not it! TypeScript provides many other features to make a developer’s life easier.

Features of TypeScript

Typescript has a rich feature set and every release comes with new features to make development easier than before. For example, with the new release (4.0), some additional features are variadic tuple types, custom JSX factories, class property inference from constructors, etc. Some typical features of TypeScript are:

Supports object-oriented programming concepts like interface, inheritance, class. generics Early detection of errors IDE support with syntax check and suggestions Easier to debug as it is typed Trans-compiles into JavaScript Used for both server-side and client-side applications Cross-platform and cross-browser support Supports all the JS libraries and extensions

Why do we need TypeScript? (Advantages of TypeScript over JavaScript)

Microsoft developed and used TypeScript for two years for its internal projects before making it public in 2012. They created a typed JavaScript because it was easier to test the code for production-grade enterprise applications. You can still use the dynamic typing features but also type the variables when it is really required. Consider the below code: We would expect the result to always be a number. But, what if a user gives “sheep”? The output will be sheep10 – ideally, we need to tell the user that this operation is not possible! Also, when you have the type information available, the text editors and IDEs become handier to use and save runtime errors. It is also easier to refactor the code at a later point in time.

Does that mean we don’t need JavaScript? (Disadvantages of TypeScript over JavaScript)

You can think of TypeScript as an extension of JavaScript, but certainly not a replacement. For smaller chunks of code, TypeScript can become an overhead – installing, compiling, trans-compiling will be redundant. With JavaScript, you can simply type your script in HTML and it would work. It is also easier to debug the code when you can simply refresh the browser every time you change something.

Head-to-head comparison

Now that we’ve understood the features and advantages of both TypeScript and JavaScript, let us go through more comparisons:

Conclusion

From a learning perspective, JavaScript could be your natural choice. You open a file, type something inside tags, save it as an HTML – you will see the results! You can then build on it to create more dynamic content. Further, when you are working on a large-scale application, having JavaScript knowledge will help you shift to TypeScript easily. However, from a career and pay perspective, as a TypeScript developer, you will be more flexible and comfortable with both JS and TS projects – so definitely a better asset in the market. Typically, TypeScript developers are paid anything between $110k-$147k, whereas JS developers are paid around $63k-$118k per annum.

Typescript vs Javascript   Understanding the Difference - 2Typescript vs Javascript   Understanding the Difference - 22Typescript vs Javascript   Understanding the Difference - 60Typescript vs Javascript   Understanding the Difference - 48Typescript vs Javascript   Understanding the Difference - 77Typescript vs Javascript   Understanding the Difference - 21Typescript vs Javascript   Understanding the Difference - 43Typescript vs Javascript   Understanding the Difference - 58Typescript vs Javascript   Understanding the Difference - 36Typescript vs Javascript   Understanding the Difference - 47Typescript vs Javascript   Understanding the Difference - 97Typescript vs Javascript   Understanding the Difference - 29Typescript vs Javascript   Understanding the Difference - 48Typescript vs Javascript   Understanding the Difference - 99Typescript vs Javascript   Understanding the Difference - 80Typescript vs Javascript   Understanding the Difference - 88Typescript vs Javascript   Understanding the Difference - 35Typescript vs Javascript   Understanding the Difference - 48Typescript vs Javascript   Understanding the Difference - 9Typescript vs Javascript   Understanding the Difference - 18Typescript vs Javascript   Understanding the Difference - 59Typescript vs Javascript   Understanding the Difference - 19Typescript vs Javascript   Understanding the Difference - 17