JS vs. TS: Should you use JavaScript or TypeScript?

By Arso Stojović
Read time 4 min
Posted on October 21, 2022
Updated on August 23, 2023
Share it on:
JS vs. TS

The main difference between JavaScript and TypeScript is their nature as programming languages. JavaScript is a dynamically typed scripting language primarily used for web development, enabling interactive and dynamic website features. In contrast, TypeScript is a superset of JavaScript that introduces static typing and enhanced developer tooling, offering a more structured approach to building applications.

"Should I learn JavaScript or TypeScript?"- is a million-dollar question, and it seems that there are a lot of different answers and opinions about it. The correct answer is that you can't learn TypeScript without learning JavaScript! Whenever you learn JavaScript, you are also learning TypeScript since the two share the same syntax and runtime features.

So, maybe the right question is, "Should I use JavaScript or TypeScript?" 

Well, the answer to this question is a bit more complicated, so let's start unraveling the tangle.

  • It takes a minute

  • Free support

  • 14 days free trial

Create your account

What is JavaScript?

It's a scripting language that helps you make interactive web pages. JavaScript runs in the user's web browser without requiring web server resources. It follows the rules of client-side programming.

JavaScript logo

The idea behind developing this script is to make it a complementary scripting language like Visual Basic was to C++ in Microsoft's language families. Although JavaScript is a powerful programming language, it is not well suited to complex applications on a large scale. A few hundred lines of code are all that it was designed for!

JavaScript features

Scripting Language

It is a lightweight language for client-side scripting in the browser. As a web-resource language, it uses a set of libraries mainly designed for web applications and is not a general-purpose language.

Interpreter-based language

The Interpreter directly executes the instructions written in the programming language without first converting them into machine code. The JavaScript language is an interpreted language similar to Ruby and Python. A browser interprets JavaScript's source code and runs it line by line. For a compiled language to be executable, it must be compiled into bytecode.

Dynamic Typing language

JavaScript is a dynamically typed language. It means that the JS interpreter does not require an explicit type definition for declaration of variables.

var dynamicValue = "a string";
console.log(dynamicValue); // Output will be "a string"
dynamicValue = 5;
console.log(dynamicValue); // Output will be number 5
          

Here, we can see the same variable dynamicValue can contain either a string or an integer with the same variable declaration.

JavaScript is a cross-platform language

JavaScript code can be executed outside web browsers using Node.js, an open-source, cross-platform JavaScript runtime environment. Streaming and real-time apps work great with Node.js, making building backends easy. JavaScript is now available in all browsers, including macOS, Linux, and Windows, thanks to Node.js. That means front end, middleware, and back end. Consequently, Node.js is part of popular stacks, such as MERN, MEVN, and MEAN.

Without Node.js, JavaScript could only run in the browser.

What is TypeScript?

TypeScript is a superset of JavaScript, which means that TypeScript is an "upgraded" version of JS. The language is statically compiled into JavaScript code. Typescript could be run on any browser if you converted it into JavaScript.

TypeScript logo

Besides static typing, TypeScript also provides classes and interfaces. Adopting Typescript for large JavaScript projects can make your software more robust and easy to develop.

TypeScript features

TypeScript Code is converted into JavaScript Code

JavaScript code makes it possible for browsers to understand and run code. There is no native way for browsers to interpret TypeScript code. As a result, TypeScript code gets compiled and converted into JavaScript. Trans-piling is a process that accomplishes this.

JavaScript = TypeScript

Changing the .js extension to .ts will convert JavaScript code into TypeScript. All JavaScript code is valid TypeScript code, but not any TypeScript code is valid JavaScript code.

JS libraries support

TypeScript code can be called directly from native JavaScript code, from JavaScript libraries, and existing JavaScript codes.

Key Difference Between TypeScript and JavaScript

Initially, JavaScript was developed for client-side programming. With JavaScript's popularity, developers also realized it could be used as a server-side programming language. JavaScript code, however, became increasingly complex as the project grew. This prevented JavaScript from meeting the requirements of large-scale applications. These factors prevented JavaScript from succeeding at the enterprise level as a server-side technology.

For this reason, TypeScript was created.

This is one of the most important differences between these two languages, but let's compare them to see the bigger picture.

 TypeScript vs. JavaScript

In addition to the differences mentioned above, it is essential to note differences in the code itself.

// JavaScript

function add(...n) {
  return n.length > 0 ? n[0] + add(...n.slice(1)) : 0;
}

const sum1 = add(2, 3, 4, 5);
console.log(sum1); // Output is number 14
const sum2 = add('2', '3', 4, 5);
console.log(sum2); // Output is string 239
          
// TypeScript

function add(...n: number[]): number {
  return n.length > 0 ? n[0] + add(...n.slice(1)) : 0;
}

const sum1 = add(2, 3, 4, 5);
console.log(sum1); // Output is number 14
/**
 * ERROR: Argument of type 'string' is not assignable
 *        to parameter of type 'number'.
 */
const sum2 = add('2', '3', 4, 5);
console.log(sum2); // Output is string 239
          

Looking at this example, you might say that the JS example is more elegant because it does not have type annotations, but the power of TS lies in the type annotation. In TS, we can see what the function add is doing without inspecting the function body; it accepts N numbers and returns a single number. In addition to this, TS will stop us from doing something like this: add('1', '2', '4', 5), because when code is compiled, we will get an error message saying that a string is not assignable to a number.

Okay, we've seen the differences, but how do they affect language choice?

TypeScript has several advantages over JavaScript

  • As part of the development process (pre-compilation), TypeScript always shows compilation errors. This makes it less likely to be affected by runtime errors than JavaScript.

  • Static typing allows TypeScript to check type correctness at compile time. This feature can also prevent possible mistakes and save time unavailable in JavaScript.

  • As a superset of JavaScript, TypeScript is easy to learn for JavaScript developers. Adopting TypeScript in your existing JavaScript projects makes the transition quicker and with fewer disruptions.

Disadvantages of using TypeScript over JavaScript

  • One of the most significant disadvantages of TypeScript is that looks much safer than it is. Doing check types automatically is a huge benefit, but also it can be deceiving if developers rely too often on this. The type check in TypeScript occurs only during compilation. After that, developers are dealing with pure JavaScript, which doesn't do that. As a result, there may still be bugs that the compiler missed; however, they'll be far fewer.

  • Because JavaScript syntax is similar to human language is widely considered one of the easiest languages to learn, along with Python and Ruby. To be honest, TypeScript can have a highly complex type structure, especially if you are a beginner. JavaScript is way more intuitive.

    Also, learning JavaScript has become a piece of cake; you can master it just by following this JavaScript Tutorial.

  • JavaScript doesn't require compilation! Instead, a browser interpreter reads the JavaScript code, interprets each line, and executes it. That is one step less in the development process.

    On the other hand, TypeScript takes time to compile the code, but that can be improved if developers skip type-checking.

JavaScript VS TypeScript: Which is better?

I would say both! TypeScript or JavaScript are both excellent choices. JavaScript vs. TypeScript has fewer differences than you think, although those differences may be significant. 

It depends on your needs and preferences. So, if you're a beginner running small scripts or simple code entirely on your own, JavaScript can be excellent, and you don't need to add additional complexity to learn to get started. 

However, if you are working on complex code bases with large teams, TypeScript can provide the necessary tools to ensure that your code can scale.

But if we speak about job opportunities and industry trends, the fact is that TypeScript is conquering the market. All "big players" are transitioning to TypeScript, so finding an (especially well-paid) job is easier. 

Another fact that can help you make decisions is if you learn TypeScript, you can work on both JavaScript as well as TypeScript projects. 

I hope I left you without any doubts; if you want to learn a new language, let your choice be TypeScript.

  • It takes a minute

  • Free support

  • 14 days free trial

Create your account