DEV Community

Cover image for TypeScript vs JavaScript
Jheyson Saavedra
Jheyson Saavedra

Posted on

TypeScript vs JavaScript

Why typescript?

TypeScript is an open source programming language, created and maintained by the Microsoft team that reinforces the shortcomings of JavaScript by giving it superpowers.

Those familiar with C # will find the use of TypeScript very familiar and this is because in both projects the administrator is Anders Hejlsberg.

TypeScript extends the features of object-oriented programming and makes it a typed language; that is, we can specify the type of our data.

for example:

let name: string = "JheysonDev";
let age: number = 15;
let isDev: boolean = true;

console.log(`${name} ${isDev ? "is a developer" : "not is a developer"} ${age} year old`)
Enter fullscreen mode Exit fullscreen mode

As we can see in the example, unlike in javascript, a : after the declaration of a variable, a function is added and then a word like number that means the data type

typescript also helps to detect errors before being interpreted as has been demonstrated in companies like google, facebook or Microsoft and in projects like vue or angular

Why javascript?

JavaScript was originally developed by Netscape's Brendan Eich under the name Mocha, which was later renamed to LiveScript, finally becoming JavaScript. The name change roughly coincided with the time when Netscape added support for Java technology in its Netscape Navigator web browser in version 2002 in December 1995. The naming caused confusion, giving the impression that the language is an extension of Java , and has been characterized by many as a Netscape marketing strategy to gain prestige and innovate in the realm of new web programming language.

JavaScript is an interpreted programming language, dialect of the ECMAScript standard. It is defined as object-oriented, prototype-based, imperative, weakly typed, and dynamic.

Unlike typescript, javascript is easier for beginners since adding types has a higher learning curve.

There are also some modules that, despite having been on the market for many years, typescript are still not compatible with it.

Conclusion

Using typescript can become somewhat tedious, especially if you are learning and do not know javascript very well but it also rewards us with a type system that helps us create better software, however both can coexist together in a project because in the end typescript depends on javascript to work

Top comments (2)

Collapse
 
willsmart profile image
willsmart • Edited

My 2c is that Javascript has a wide array of traps for inexperienced devs to step into, and many/most of these are no longer issues when the dev is working in Typescript.
For example,

  • what does this refer to at any point in time?
  • type coercion rules causing problems
  • expecting an undefined from a system function that returns null

Even just the fact that object keys are reported as strings is enough to trip a coder up...

const barChart = {1:1, 2:4, 3:3},
      indexForTop = Object.entries(barChart).reduce(([k1,v1], [k2,v2])=> v1>v2 ? [k1,v1] : [k2,v2])[0] 
Enter fullscreen mode Exit fullscreen mode

Typescript will let you know that indexForTop is a string.

If coding is like putting a new roof on a house, having better static analysis tools, like type safety or good linting, is like setting up a better scaffold around the house before you start.
It is a pita in many ways but I'd think (apples to apples) that novice roofers need that safety more than the experienced ones.

Collapse
 
jheysonsaav profile image
Jheyson Saavedra • Edited

Correct