TypeScript vs. JavaScript: Which One Should Use, and Why?

Quokka Labs
Tech and Tricks
Published in
6 min readDec 15, 2022

This is the most frequently asked question among mobile app and front-end developers: Which is good? JavaScript vs. TypeScript.

Don’t worry! You will get the best answer when deciding which to use. This blog will compare TypeScript vs. JavaScript and explain when to use each. We will also cover all the most important things that will help you choose.

Let’s get started!

What is JavaScript? (As Fast as Possible)

JavaScript is a widely-used, cross-platform programming language essential for front-end and back-end development. With JavaScript, you can build various applications, including mobile, desktop, and web apps, back-end servers, and interactive apps.

Today, a newer version of JavaScript, such as ECMAScript 12, is available, which offers improved execution, performance, and interaction with the language. Front-end developers generally use it as a fundamental language, as it is used in HTML and CSS.

What is TypeScript? (As Fast as Possible)

TypeScript is also a programming language with all the features of JavaScript and other additional features.

You may be thinking, “Why TypeScript, then?” Because of some quirks and issues, Microsoft created TypeScript in 2012. The main difference will be discussed further below.

What’s The Difference: JavaScript Vs TypeScript (Main Differences)

Based on the differences listed above, it is clear that TypeScript has advantages over JavaScript.

What Are the Advantages: TypeScript vs. JavaScript

TypeScript has recently approached the top 5 programming languages because it is the most widely used, provides new features to developers, and has a fantastic community. Also, TypeScript listened more carefully to developers’ needs compared to JavaScript.

  • TypeScript’s most advantage is the compiler that checks errors and types. The compiler shows errors in real time. It’s useful while refactoring the code and shows you that you may be missed.
  • If TypeScript is paired with IDE, TypeScript can provide more great features like hinting and code completion. It helps speed up development.
  • If a developer uses a tsconfig file, then TypeScript provides an option to transpile the code to the previous version. That means the code can run on all browsers.
  • TypeScript can run parallel with JavaScript code, making it easy to implement JavaScript files in legacy projects.
  • TypeScript has features that make code more fun and easily readable. Features like interfaces, Enums, and generics make code interesting.

Advanced Code Suggestions

/**
* Easy function to parse a CSV incorporating individual's info.
* @param data A string containing a CSV with 3 fields: name, surname, and age.
*/
const parsePeopleData = (data: string) => {
const people: {name: string, surname: string, age: number}[] = [];
const errors: string[] = [];

for (let row of data.split('\n')){
if (row.trim() === '') continue;

const tokens = row.split(',').map(i => i.trim()).filter(i => i != '');
if (tokens.length < 3){
errors.push(`Row "${row}" contains only ${tokens.length} tokens. 3 required`);
continue;
}
people.push({ name: tokens[0], surname: tokens[1], age: +tokens[2] })
}
return {people, errors};
};

const exampleData = `
Gordon,Freeman,27
G,Man,99
Alyx,Vance,24
Invalid Row,,
Again, Invalid
`;

const result = parsePeopleData(exampleData);
console.log("Parsed People:");
console.log(result.people.
map(p => `Name: ${p.name}\nSurname: ${p.surname}\nAge: ${p.age}`)
.join('\n\n')
);
if (result.errors.length > 0){
console.log("\nErrors:");
console.log(result.errors.join('\n'));

In the above code, TypeScript suggests autocompleting the keys’ names.

Interface Support

export class PersonInfo {
constructor(
public name: string,
public surname: string,
public age: number
){}
}

export interface ParserStrategy{
/**
* Parse a line if able.
* @returns The parsed line or null if the format is not recognized.
*/
(line: string): PersonInfo | null;
}

export class PersonInfoParser{

public strategies: ParserStrategy[] = [];

parse(data: string){
const people: PersonInfo[] = [];
const errors: string[] = [];

for (let row of data.split('\n')){
if (row.trim() === '') continue;

let parsed;
for (let s of this.strategies){
parsed = s(row);
if (parsed) break;
}
if (!parsed){
errors.push(`Unable to find a strategy capable of parsing "${row}"`);
} else {
people.push(parsed);
}
}
return {people, errors};
}
}


const exampleData = `
Gordon,Freeman,27
G;Man;99
{"name":"Alyx", "surname":"Vance", "age":24}
Invalid Row,,
Again, Invalid
`;

const parser = new PersonInfoParser();

const createCSVStrategy = (fieldSeparator = ','): ParserStrategy => (line) => {
const tokens = line.split(fieldSeparator).map(i => i.trim()).filter(i => i != '');
if (tokens.length < 3) return null;
return new PersonInfo(tokens[0], tokens[1], +tokens[2]);
};

parser.strategies.push(
(line) => {
try {
const {name, surname, age} = JSON.parse(line);
return new PersonInfo(name, surname, age);
} catch(err){
return null;
}
},
createCSVStrategy(),
createCSVStrategy(';')
);

const result = parser.parse(exampleData);
console.log("Parsed People:");
console.log(result.people.
map(p => `Name: ${p.name}\nSurname: ${p.surname}\nAge: ${p.age}`)
.join('\n\n')
);
if (result.errors.length > 0){
console.log("\nErrors:");
console.log(result.errors.join('\n'));
}

As you can see in the above code, TypeScript supports and implements a common OOP pattern very cleverly.

What are the Disadvantages: TypeScript vs. JavaScript

We know the advantages of TypeScript compared to JavaScript; now, let’s see the disadvantages as quickly as possible. But it is good to see that the disadvantages are okay, but new mobile app developers or front-end developers should know about them.

  • The biggest disadvantage is relying on the compiler more than a developer should. Developers rely too much on compilers guessing that the compiler will flag all the issues. But it’s not true. Well, it’s better than JavaScript, but it’s not that bulletproof or bug-proof. In other words, TypeScript can help find more bugs (15% more) than JavaScript.
  • Another disadvantage is that TypeScript needs more compilation steps, which may slow the build process.
  • Another issue with TypeScript is that if it receives a new feature, it may increase the learning curve for a front-end developer.
  • The last disadvantage is that developers must do extra code to define the types. It saves time if we see it in the long run, but it consumes time in the short term. So, it’s an advantage for the long term but consumes time in the short term.

TypeScript: When To Use And When To Not

When Is TypeScript Appropriate for a New Development?

It’s natural to be skeptical when you first start using a new technology, but if it has advantages over your previous technology, it’s worth a shot.

If you are on a new development project and you will be the only one working on it, then TypeScript will be a good choice for your next mobile app development. The initial development phase will be time-consuming and tricky, but you will eventually like working with TypeScript.

But if there are more teammates, managing your development project will be slightly more difficult, and you will need constant consent for code changes. It slows down the development process in the initial phase, but in the long term, it can benefit us.

When Should You Not Use TypeScript for a New Development?

There are occasional reasons why you should take care of it before using TypeScript. The first and biggest reason is if you have a tight deadline. It will be stressful and unproductive if you are learning this new technology and have a tight deadline.

Also, TypeScript is challenging to configure, especially for beginners. Also, it takes time to install multiple npm libraries. In a nutshell, TypeScript raises the time of entry threshold.

JavaScript vs. TypeScript: Project Migration

Migrating a project written in JavaScript to TypeScript can give a mobile app and front-end developers many advantages in the development life cycle. It also finds bugs that are not noticed and streamlines project maintenance.

It is simple to migrate if you are working on a small project. Install the required libraries, rename file extensions from.js to.ts, and let the TypeScript compiler fix the errors.

But if more JavaScript fields exist in the project, it will be complicated to migrate. You can migrate some files as a priority and run them in parallel with the JavaScript code.

Another option is also a good one. Just add new features by using TypeScript. The best thing is that it only interacts with a part of legacy code.

The VERDICT: JavaScript vs. TypeScript

TypeScript is a great object-oriented language that helps us to build large apps faster and more efficiently. However, it had drawbacks, but in the long run, TypeScript is advantageous.

If you work alone, I recommend going for TypeScript, and if you work in a team, then it’s good to build TypeScript projects in your free time and collaborate with the team to explain them. That’s it.

Don’t worry if you are stuck somewhere migrating a project or have a market-changing idea for a new project; reach out to us to finalize it.

--

--

Quokka Labs
Tech and Tricks

Quokka Labs is a Web/Mobile App Development Company in India. Founded by a team of Technology enthusiasts, our goal is to create digital products of the highest