DEV Community

Cover image for Does React Use JavaScript or Typescript?
Udemezue John
Udemezue John

Posted on

Does React Use JavaScript or Typescript?

Introduction.

When you're building modern web applications, you've likely come across React.

As one of the most popular JavaScript libraries for building user interfaces, React has a huge developer community, great resources, and powerful tools to offer.

But if you're new to it or exploring how to get the most out of it, one question often comes up: should you use JavaScript or TypeScript with React?

Both JavaScript and TypeScript can be used to create React applications, but there are key differences between the two that can influence your development process.

JavaScript, as the default option, is more familiar to many developers and has been the backbone of web development for years.

On the other hand, TypeScript, which is a superset of JavaScript, adds static typing, offering additional safety and tooling to the development process.

In fact, according to the 2023 State of JS survey, TypeScript is used by 69% of JavaScript developers in some form, highlighting its growing popularity in the web ecosystem.

So, why would developers choose one over the other when working with React? In this post, I’ll break down the pros and cons of using JavaScript versus TypeScript in a React environment, and help you decide which is best for your next project.

Does React Use JavaScript or Typescript?

If you’re working with React or thinking about learning it, one of the key questions that comes up is whether React uses JavaScript or TypeScript.

This choice significantly impacts the way you structure your code, how quickly you can spot errors, and how scalable your application can become.

Let’s break down the difference between JavaScript and TypeScript in the context of React, and explore why one might be preferable over the other depending on the scenario.

React and JavaScript: The Original Pairing

React was originally built with JavaScript in mind. JavaScript is the language of the web, and React, as a JavaScript library, was designed to create interactive user interfaces in a dynamic and efficient way.

Many developers still use React with pure JavaScript, and it’s perfectly fine to do so.

JavaScript is flexible, dynamically typed, and widely used. React thrives on this flexibility, enabling rapid prototyping and the ability to easily work with third-party libraries.

In JavaScript, variables don’t require explicit types, making it quicker to write code but leaving more room for potential bugs down the line.

When writing React components with JavaScript, you’ll often see code like this:

function Greeting(props) {
  return <h1>Hello, {props.name}</h1>;
}

Enter fullscreen mode Exit fullscreen mode

This is simple, concise, and works. However, JavaScript doesn’t check the types of props or any function parameters, so errors can sometimes only be caught at runtime after they've affected your app.

The Growing Popularity of TypeScript in React

TypeScript, on the other hand, is a statically typed superset of JavaScript.

This means that TypeScript builds on JavaScript by adding type definitions, allowing developers to catch more errors during development rather than waiting until runtime.

Over the last few years, TypeScript has gained significant traction, especially in the React ecosystem.

According to the 2023 Stack Overflow Developer Survey, 46% of developers prefer TypeScript over JavaScript when working with modern JavaScript frameworks, including React.

A big reason for TypeScript’s rising popularity is that it enhances code reliability.

With TypeScript, you can define the types of your variables, function parameters, and return values, making your code more predictable and robust. Here’s an example of the same Greeting component rewritten in TypeScript:

interface GreetingProps {
  name: string;
}

function Greeting({ name }: GreetingProps) {
  return <h1>Hello, {name}</h1>;
}

Enter fullscreen mode Exit fullscreen mode

In this example, I’ve defined an interface GreetingProps that enforces name as a string. If you try passing a number or another type as name, TypeScript will flag an error before the code even runs.

This makes catching bugs earlier easier and provides better tooling, such as autocompletion and refactoring support.

Why Use JavaScript for React?

  • Simplicity and Flexibility: JavaScript is incredibly flexible. If you’re working on a smaller project or prototype, the extra setup and type-checking might seem unnecessary. JavaScript allows you to quickly iterate and experiment without worrying about types.
  • Smaller Learning Curve: If you’re just getting started with React or front-end development in general, sticking with JavaScript can be easier. Learning both React and TypeScript at the same time can feel overwhelming.
  • Broad Ecosystem Support: Many React libraries and tools are built with JavaScript in mind, although TypeScript support is becoming increasingly common.

For developers looking for maximum flexibility, JavaScript allows for fast development, especially in the early stages of a project or in environments where rapid iteration is key.

Why Use TypeScript for React?

  • Improved Developer Experience: TypeScript’s type checking catches many common errors at compile-time. This reduces bugs in production and saves debugging time. IDEs like Visual Studio Code also provide richer intellisense (code suggestions) when working with TypeScript.
  • Scalability: TypeScript shines when working on large-scale applications. As your React app grows in complexity, keeping track of the types of props, state, and other variables can be challenging. TypeScript helps maintain clarity, especially in a team environment, by enforcing clear contracts for your components.
  • Refactoring and Maintenance: Refactoring code in a JavaScript project can be risky without extensive tests. TypeScript makes it safer to refactor components because it will alert you if a type mismatch occurs. This is a huge benefit in long-term maintenance.
  • Better Collaboration: When working in a team, TypeScript can make the codebase more consistent and easier to understand for all developers, even those unfamiliar with a specific part of the code. The type annotations provide clear documentation, helping everyone know what to expect from functions and components.

Which Should You Use?

There’s no universal answer to this. Both JavaScript and TypeScript can be great for building React applications, but the decision depends largely on the size and scope of your project, your team’s familiarity with TypeScript, and your personal preference for writing types.

If you’re building a small project, want rapid iteration, or are just starting out with React, JavaScript may be the better choice. You’ll get up and running faster without the overhead of learning and setting up TypeScript.

However, if you’re working on a larger, more complex application that needs to be maintained over the long term—or if you want the peace of mind that comes with catching errors earlier—TypeScript is likely the better option.

How to Start with TypeScript in React

If you’ve decided to give TypeScript a try, the good news is that it’s fairly easy to get started.

React’s official create-react-app tool supports TypeScript out of the box. Here’s how you can initialize a new React project with TypeScript:

npx create-react-app my-app --template typescript

Enter fullscreen mode Exit fullscreen mode

This will set up a new React project with all the necessary TypeScript configurations in place, so you can start adding type definitions to your components right away.

Conclusion.

React can work seamlessly with both JavaScript and TypeScript. If you value flexibility and want a quick development process, sticking with JavaScript might be the way to go.

But if you want stronger type safety, improved code maintainability, and better long-term scalability, TypeScript will serve you well in the React ecosystem.

Top comments (0)