Course contentsShow
← TypeScript
Lesson 52 of 1,7962. Setup and ToolingFree lesson

target: Choosing Your Output JavaScript Version

Setting the target ECMAScript version (ES5, ES2015, ES2020, ESNext) for the compiled JavaScript output.

What You'll Learn

You'll learn how to control which version of JavaScript TypeScript compiles your code into using the target compiler option.

Why This Matters

TypeScript code doesn't run directly in browsers or Node.js—it gets compiled into plain JavaScript first. But which flavor of JavaScript? That's where target comes in.

Think of it like a translator who can speak to people from different time periods. Modern JavaScript (ES2020+) has sleek features like optional chaining (?.), but older browsers don't understand it. Setting target tells TypeScript: "translate my code so this version of JavaScript can understand it."

The Core Concept

In your tsconfig.json, the target option lives inside compilerOptions:

{
  "compilerOptions": {
    "target": "ES2020"
  }
}

Common target values:

  • ES5: Maximum compatibility (IE11, very old browsers)
  • ES2015 (or ES6): Modern baseline (arrow functions, classes)
  • ES2020: Recent features (optional chaining, nullish coalescing)
  • ESNext: Absolute latest JavaScript proposals

Choosing Your Target

Ask yourself: "Where will this JavaScript run?"

  • Building for old browsers? Use ES5
  • Modern web apps with build tools? ES2015 or ES2020
  • Latest Node.js versions? ES2020 or ESNext

Higher targets produce cleaner, smaller code but require modern environments. Lower targets work everywhere but include more "polyfill" code that mimics newer features.

Example Impact

Your TypeScript const greet = () => "Hello" becomes:

  • ES2015: const greet = () => "Hello"; (unchanged)
  • ES5: var greet = function() { return "Hello"; }; (transformed)

Key Takeaway: The target option determines which JavaScript version TypeScript compiles to—choose based on where your code will actually run.