DEV Community

Cover image for JavaScript v TypeScript
JavaCode7
JavaCode7

Posted on • Edited on

JavaScript v TypeScript

JavaScript. By far one of the most famous programming languages for its applications in scripting, gamedev and most notably web apps 🌐. Then its brother, TypeScript. Famous for being statically typed. It adds more features to JavaScript. Which one is better? 🤔 I guess today we find out.

Disclaimer

What follows is going to be my opinion inferred from fact. This may or may not be true. Please read this article accordingly.

JavaScript

JavaScript has many features, scripting, web apps and is even supported in some game engines. To sum it up nicely:

  • ✔  Dynamically typed (beginners don't mix up typing)
  • ❌ No optional parameters
  • ❌ No interfaces
  • ✔  Some game engine support
  • ✔  Compiled not transpiled
  • ❌ No TS file imports

TypeScript

TypeScript has similar syntax to JavaScript but with a few features added.

  • ❌ Statically typed
  • ✔  Optional parameters
  • ✔  Interfaces
  • ❌ Transpiled
  • ✔  Both TS and JS imports
  • ✔  Most JS frameworks support TS

Based on all of this, and my experience using both languages. I would say that TypeScript is better than JavaScript. You may disagree however. If you do, please comment 💬! I would love to hear your opinions!

Top comments (22)

Collapse
 
pranavbaburaj profile image
Pranav Baburaj

No public or private keywords
Hey, I think you have mistaken.
The private keyword is used to hide a method or a property in a class from. To do this in js, you can use

class Hello {
    _private_function(){}
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
peerreynders profile image
Collapse
 
kwiat1990 profile image
Mateusz Kwiatkowski

Regarding new proposal: why to use # instead of private keyword for the private members? Or am I missing something here?

Thread Thread
 
peerreynders profile image
peerreynders

FAQ:

This sort of declaration is what other languages use (notably Java), and implies that access would be done with this.x. Assuming that isn't the case (see below), in JavaScript this would silently create or access a public field, rather than throwing an error. This is a major potential source of bugs or invisibly making public fields which were intended to be private

Collapse
 
javacode7 profile image
JavaCode7

Ok...

Collapse
 
pranavbaburaj profile image
Pranav Baburaj

:slight_smile:

Thread Thread
 
javacode7 profile image
JavaCode7

I think you mean 😊

Collapse
 
jwhenry3 profile image
Justin Henry

Typescript is used in Unity, Phaser, Excalibur. I'm sure there are more, but the listing of Typescript not having Game Engine Support is inaccurate.

Collapse
 
javacode7 profile image
JavaCode7 • Edited

Ok but I thought Unity removed UnityScript?

Collapse
 
jwhenry3 profile image
Justin Henry

forum.unity.com/threads/does-unity...
It appears it is deprecated but not removed yet, but that does not mean we should use UnityScript until it's gone, so it's a fair point. Aside from Unity, most JS game engines come with either their own typings or have 3rd party typings available. Assuming you have a decent IDE and a project setup, you should be able to use Typescript with any JS game engine (Though without the support, like you mentioned).

Collapse
 
mohdahmad1 profile image
Mohd Ahmad

I like Typescript a lot.

Collapse
 
javacode7 profile image
JavaCode7

As do I. I like the static typing.

Collapse
 
mohdahmad1 profile image
Mohd Ahmad

and intelliscense, typescript provide in vscode

Thread Thread
 
javacode7 profile image
JavaCode7

It does. 😅

Collapse
 
peerreynders profile image
peerreynders

I would say that TypeScript is better than JavaScript.

From TypeScript is not JavaScript:

Also TypeScript does not shield you from having to deeply understand JavaScript. A TypeScript expert must also be a JavaScript expert!

TypeScript is essentially a JavaScript superlinter that requires more information than ESLint.

One has to become competent in JavaScript one way or another - TypeScript or not. The official TypeScript documentation doesn't cover the JavaScript foundation that TypeScript is based on.

Some projects are using TypeScript to develop "typed JavaScript":

Collapse
 
javacode7 profile image
JavaCode7

This is true but they are different enough that people can have a preference. Hence this article 😃.

Collapse
 
lexlohr profile image
Alex Lohr

There are a few mistakes. With Babel, you can import TypeScript directly from within JavaScript, though it needs a bit of setup. In JavaScript, all parameters are optional by default, because their types are obviously not enforced. JavaScript is only JiT-compiled, otherwise an interpreted language.

Collapse
 
jwhenry3 profile image
Justin Henry

With Deno coming along, there will be a time when typescript gets executed the same way javascript does, as Deno has first-class support for typescript, which would at some point eliminate the Transpiled negative depending on the use case.

Collapse
 
javacode7 profile image
JavaCode7 • Edited

I will remove it when the time comes then!

Collapse
 
jesse1981 profile image
Jesse Bryant

Short & sweet article.

My only comment is while JS doesn't have optional parameters "natively", it can still be treated that way. It won't complain that you didn't provide a value for a parameter, and you can structure you code to see if a parameter is given or undefined.

Does that sway your opinion of it being better than TS? - probably not. Just sayin 😏

Collapse
 
stojakovic99 profile image
Nikola Stojaković • Edited

✔ Compiled not transpiled

Well, technically JS is both compiled and interpreted. Read this answer for more details.

Collapse
 
javacode7 profile image
JavaCode7

But isn't JS more compiled than interpreted (sorry if I'm wrong, I'm not the most experienced JS dev)