JavaScript and TypeScript

Typescript vs Javascript

Posted in JavaScript, TypeScript
Typescript vs Javascript

If you have ever worked on a web development project, you must have seen what JavaScript is like. JavaScript has been there for a long time now as the most popular scripting language for many web projects.

Typescript is an open-source programming language most suited for large applications. It was developed by Microsoft in 2012, mainly because JavaScript code was becoming too complex to handle when it comes to large-scale applications.

Difference between Typescript and Javascript

Essentially, all your JavaScript code is also valid in Typescript – that means Typescript is a superset of JavaScript – or

JavaScript + more features = Typescript.

So, if you save your JavaScript (.js) file with a Typescript (.ts) extension, it will work perfectly fine. But that does not mean that Typescript and JavaScript are the same.

Before outlining the differences between both, let us understand what each language looks like!

JavaScript

JavaScript is one of the most popular core technologies of the web. From the beginning, it has been an integral part of web applications making web pages interactive and dynamic. It is a high-level language, with JIT (Just-in-Time) compiler and dynamic typing. For long, JS was a client-side implementation, but some newer JS engines also have server-side implementations. The syntax of JS is very similar to Java, and so are the standard libraries. As a starting point, JS is the best scripting language to learn. Check some nice tutorials here.

JavaScript was developed by Netscape in collaboration with Sun Microsystems.

Some unique features of JavaScript are –

  • Flexible, dynamic and cross-platform
  • used for both client-side and server-side
  • Lightweight interpreted
  • Supported by all browsers
  • Weakly typed
  • JIT compilation

Let us take a simple example to illustrate how JavaScript works. The following simple HTML code is to validate a username field where myFunction() is written in JavaScript.

If you observe, the syntax of the functions is similar to Java, however, we have defined the variables as ‘var’ and not declared them as any type. The myFunction() function is triggered when the user clicks on a ‘Submit’ button and gives appropriate alert messages as per the conditions. Here is sample output –

It is that simple! If you know Java, JavaScript is fairly simple to learn.

TypeScript

TypeScript is no different from JavaScript in its purpose but is used for developing large applications. TypeScript trans compiles (source to source compilation) to JavaScript. It follows an object-oriented programming language structure and supports its features like classes, interfaces, namespaces, and inheritance. Static typing is possible in TypeScript through type annotations (numbers, string and boolean). For example,

class Student {
private name: string;
}

As we see above, TypeScript is strongly typed. This makes it better to debug (during compile time itself) which is a more efficient way to code for large projects. TypeScript program typically consists of modules, functions, variables, comments, expressions, and statements – just like any other full-fledged programming language. Some prominent features of TypeScript are –

  • Easy to maintain and enhances project productivity
  • Static typing and annotations are possible
  • Supports object-oriented features like interface, inheritance, and classes
  • Easy to debug and early detection of errors
  • Supports ES6 (ECMAScript) that offers easier syntax to handle objects and inheritance features
  • The good full-fledged IDE support

Does that make TypeScript better than JavaScript?

Before further comparison of TypeScript and JavaScript, another important question needs to be addressed! Since TypeScript is a superset of JavaScript, should we always use Typescript? Does being a superset makes TypeScript suitable for all types of projects?

No.

TypeScript is no way to replace or make JavaScript obsolete. JavaScript is still the most favorite client-side scripting language. For smaller projects, using TypeScript could be an overhead because it takes time to trans compile the code into JavaScript, which is an extra step.

JavaScript is directly run on the browser, so for small code chunks, it's easier to refresh and debug the code. In the case of TypeScript, we need a proper IDE and set up to run the code.

When should you migrate your project to TypeScript?

When the code becomes huge, complex to handle, and more prone to errors, it is better if some errors are caught during compile time itself. That is where TypeScript helps. The beauty is that the entire codebase written in Java can be reused as such.

Typescript vs Javascript: Head to head comparison

Now as we understand the basic features and purpose of both, let us explore some more low-level differences apart from what we have already learned so far –

TYPESCRIPT JAVASCRIPT
Superset of JavaScript developed to overcome code complexity for large projects A scripting language that helps create dynamic web page content
Errors can be found and corrected during compile time Errors can be found only during run-time as it is an interpreted language
Strongly typed, supports both static and dynamic typing Weakly typed, no option for static typing
Converted into JavaScript code to be understandable for browsers Can be directly used in browsers
Since it is a superset, all the JavaScript libraries, and other JavaScript code works without any changes JS libraries work by default
There is support for ES3, ES4, ES5 and ES6 No support for compiling additional ES3, ES4, ES5 or ES6 features
Supports modules, generics and interfaces to define data No support for modules, generics or interface
Functions can have optional parameters This feature is not possible in JavaScript
An example code –
function multiply (a, b) {
return a*b;
}
var result = multiply(a, b);
console.log(‘The answer is - ’ + result);
Example code –
<script>
function multiply (a, b) {
return a*b;
}
var result = multiply(a, b);
document.write (‘The answer is – ’ + result);
</script>
Numbers, Strings are considered as interfaces. Number, string are objects.
Powerful and intuitive language Neat and clean, most suitable for simple web applications
The community support is still growing and not so huge Huge community support with lots of documentation and support for solving issues
Prototyping is possible Prototyping support is not there
Takes time to learn and code, scripting knowledge is a must. Can be learned on the go, no prior scripting experience is needed.
Proper build setup (npm package) is required for static type definitions No build setup is required

Conclusion

As we have already determined, JavaScript is most suited when your team is new and is working on small web projects. If you have a team with good expertise and knowledge and want them to handle a complex project, going for TypeScript is a perfect choice. That said, if you are looking for a learning curve and job opportunities, definitely TypeScript has an edge over JavaScript. TypeScript developers are paid an average salary of $148,000 per year whereas JS developers are typically paid around $110,000 per year. If you learn TypeScript, you can work on both JS as well as TypeScript projects. Start learning TypeScript here.

People are also reading:

Ramya Shankar

Ramya Shankar

A cheerful, full of life and vibrant person, I hold a lot of dreams that I want to fulfill on my own. My passion for writing started with small diary entries and travel blogs, after which I have moved on to writing well-researched technical content. I find it fascinating to blend thoughts and research and shape them into something beautiful through my writing. View all posts by the Author

Leave a comment

Your email will not be published
Cancel
Karen Wood
Karen Wood

When should you not use TypeScript?

Glenn Shaw
Glenn Shaw

TypeScript is not complete in itself; you have to have basic knowledge of JavaScript. By using TypeScript you will face some risks that are not there in JavaScript. TypeScript somehow limits the benefits of JavaScript and obscures its strong sides. It is not completely open-sourced as claimed, it is stilled controlled by Microsoft.

Meghan Massey
Meghan Massey

How do I run a TypeScript file?

Jack Schmidt
Jack Schmidt

Basically you cannot directly run the TypeScript code in the browser. First, you need a run-time environment like NodeJs. Whenever you write the code it gets compiled recent version of the JavaScript. You can use Babel to convert the code in JavaScript that will be compatible with the browser. You can then use webpack for real projects if required. All these tools are required if you are going to run the TypeScript code.

Angela Hoffman
Angela Hoffman

Can we use alert () in TypeScript?

Maria Wright
Maria Wright

Yes, we can use alert in TypeScript, it is often used to make sure that information comes from the user and it displays some message to the user. It can be an alert message, confirmation message or any prompt information.

Lee Gibson
Lee Gibson

How do I run TypeScript in the browser?

Toby Banks
Toby Banks

Basically you cannot directly run the TypeScript code in the browser. First, you need a run-time environment like NodeJs. Whenever you write the code it gets compiled recent version of the JavaScript. You can use Babel to convert the code in JavaScript that will be compatible with the browser. You can then use webpack for real projects if required. All these tools are required if you are going to run the TypeScript code.

Terri Rios
Terri Rios

How do I practice TypeScript?

Kimberly Nichols
Kimberly Nichols

If you want to get along with TypeScript then you should be learning the basics first. The best way to learn and practice about basics is by developing some simple applications that will give you hands-on experience. You can even go for online editors to practice your codes and helps you learn the concept.

Santos Berry
Santos Berry

Can you mix TypeScript and JavaScript?

Walter Summers
Walter Summers

Latest with Babel 7 you can mix the JavaScript with TypeScript with the help of babel-loader and TypeScript loader. Choose the right loader for your project and this process combines both the compilers and merge the TypeScript output with pure JavaScript in your codebase. Then the output is given to the babel to get the ES5 code to run.

Gerardo West
Gerardo West

Is TypeScript slower than JavaScript?

Kelly Hines
Kelly Hines

Though TypeScript is a superset of JavaScript means whatever you write in TypeScript get converted into JavaScript. If we consider the compilation of the code then TypeScript works faster as the code gets compiled easily and faster. Typescript code converted to JavaScript.

Inez Carroll
Inez Carroll

Should I use TypeScript or es6?

Leo Howell
Leo Howell

TypeScript is the superset of JavaScript while es6 is the next generation syntax of JavaScript. TypeScript provides typing, es6 additional features with great tool support. All the features combined makes the project much compact and clear, whatever the size of the project. But there is always an advantage of using Typescript for small developments.

Rosie Ramsey
Rosie Ramsey

Is TypeScript a transpiler?

Israel Reed
Israel Reed

Yes, TypeScript is a transpiler. It means that one high-level programming language source code is converted into another high-level programming language. It is done by a tool that is called a transpiler. Some documents referred it to as the TypeScript compiler and the process is called compilation.

Bradley Copeland
Bradley Copeland

Does TypeScript improve performance?

Miriam Carlson
Miriam Carlson

Yes, TypeScript improves performance as the code is compiled into the minimized form and therefore code runs faster. It is somehow slower to code but it saves time by prompting the errors so you can debug them within no time. Code is much easier to understand and makes it time saver for a programmer.