in

Typescript vs Javascript – Understanding the Difference

default image

Hey there!

As a fellow developer, I know you‘ve likely wondered about the differences between JavaScript and TypeScript. I‘ve built apps with both and want to share my perspective to help you decide when to use each.

First up, JavaScript is the tried and true scripting language we all rely on for web development. Here‘s a quick refresher on what makes it so ubiquitous:

  • JavaScript first emerged in 1995 and now runs in every major web browser. This cross-platform nature is key.

  • JS allows us to make websites dynamic and interactive without needing page reloads. We can directly manipulate DOM elements.

  • Clean syntax and a relatively gentle learning curve made JS accessible for beginners.

  • But JS has evolved a lot since the early days. The addition of async/await, arrow functions and other features make it powerful for complex apps.

So JavaScript really paved the way for rich application-like experiences on the web. And with Node.js, JS expanded beyond the browser into serverside development as well.

Now, TypeScript came onto the scene much more recently – first released by Microsoft in 2012.

It builds on JavaScript‘s capabilities with some key enhancements:

  • TypeScript most notably adds optional static typing. This gives us compile-time type checking for greater stability.

  • The TypeScript compiler outputs clean, simple JavaScript code that runs anywhere JS runs.

  • TypeScript incorporates new features from upcoming JavaScript versions years before they may be fully supported in environments like browsers. The compiler handles any necessary down-level compilation for us.

  • Things like interfaces, enums, generics, and modules are welcome additions for larger-scale development.

So in summary, TypeScript takes JavaScript to the next level for truly immense apps and teams. For small to mid-sized projects, either may suffice.

To dig deeper into why you may prefer one over the other, let‘s look at some key differences:

Static vs. Dynamic Typing

With JavaScript, variables can freely change types. This flexibility comes at the cost of runtime errors creeping in.

But TypeScript checks types at compile time. This means I can catch a whole class of errors early on.

Studies have found up to 15% of code defects can be attributed to type errors. So TypeScript prevents many bugs from ever reaching users.

Of course, static typing requires some adaptations like type declarations. But for smooth sailing and safety at scale, I‘ve come to appreciate it.

Compilation Step

TypeScript needs an extra compilation phase to output JavaScript. So there‘s a little more startup effort to integrate a compiler like TSC.

However, compilers open the door for powerful tooling. I can augment my editor with improved autocomplete, inline docs, and error flagging visible right in the code.

These facilities make development a lot less stressful! Finding issues earlier keeps me in that state of coding "flow" more consistently.

Framework Usage

I‘ve built apps with React, Angular, and Vue. TypeScript seems to have more traction among Angular developers.

That‘s likely because Angular itself is written in TypeScript. So it integrates cleanly with the framework architecture.

Over 70% of Angular developers reported using TypeScript in the 2020 StackOverflow survey. Adoption among React devs was lower at 39% as React doesn‘t mandate TypeScript.

So framework choice can nudge you towards one language or the other. But many devs mix and match both within projects.

Learning Curve

For those totally new to web development, JavaScript remains a friendlier starting point. You can instantly write and run scripts without any compiling or build setup.

However, for experienced JavaScript devs like yourself looking to level up on complex apps, TypeScript should feel like a natural progression.

The learning curve lies more in grasping how to properly apply typing. The core syntax still builds cleanly on your existing JS foundation.

Long Term Maintenance

This is a big factor for large and enterprise apps. And something we all have to think about as our projects grow!

Type annotations serve almost like documentation and clarify what functions or classes expect. It‘s easier for new team members to parse and contributes to safer refactors.

One survey found 37% of developers reported TypeScript made maintaining and debugging code easier. The compiler guardsrails help keep complexity manageable.

Beyond our own experiences, we can look at usage stats to spot trends:

  • The State of JS 2020 survey found TypeScript usage more than doubled from 2016 to 2020. Now at 39% of respondents.

  • The StackOverflow 2020 survey saw TypeScript rise to the #4 spot for "Most Loved" languages while JavaScript remained at #1 overall.

  • On GitHub, TypeScript now powers over 172,000 public repositories compared to just 16,000 in 2016.

So while JavaScript remains dominant, TypeScript is rapidly gaining ground especially amongst seasoned JavaScript developers. Its large-scale benefits lead teams to introduce it incrementally.

Recommendations

With both languages advancing, we have more choice than ever. Here are my guidelines based on past experience:

  • For straight forward website scripts and DOM manipulation, JavaScript may be all you need.

  • On mid-sized apps, either TypeScript or JavaScript can work well. Choose based on team preferences.

  • For complex enterprise web apps, I‘d highly recommend TypeScript for the stability, longevity and tooling.

  • If relying heavily on classes and advanced OO patterns, TypeScript will integrate cleanly for you.

  • For beginners just looking to learn web basics, JavaScript is likely the best starting point.

  • For experienced JavaScript devs on bigger projects, TypeScript is an easy ramp up.

So in closing my friend, hopefully this gives you a 360 overview of how JavaScript and TypeScript compare today. Let me know if you have any other questions!

AlexisKestler

Written by Alexis Kestler

A female web designer and programmer - Now is a 36-year IT professional with over 15 years of experience living in NorCal. I enjoy keeping my feet wet in the world of technology through reading, working, and researching topics that pique my interest.