Imagine you’re writing a letter in modern English slang (“That’s lit, fam!”). Now, you want your great-grandparents to read it—but they only understand old-fashioned English. You’d need to “translate” your slang into words they understand.

That’s what transpiling is in JavaScript:

Taking modern code (newer JavaScript features like ES6/ES2020) and converting it into an older version of JavaScript that all browsers—even old ones—can understand.


🧑‍💻 Why Do We Need Transpiling?

  • Browsers differ: Not all browsers support the latest JavaScript features.
  • Compatibility: Old browsers may break if you use new syntax.
  • Future-proofing: Developers can use modern, cleaner code without worrying about whether every user’s browser supports it.

🛠️ How Do We Transpile?

We use special tools like:

  • Babel (the most popular transpiler)
  • Traceur (earlier Google project)

These tools read your new JavaScript and rewrite it into older JavaScript.


📝 Example

Modern ES6 code:

// Using arrow functions and let
let add = (a, b) => a + b;
console.log(add(2, 3));

After transpiling into ES5 (older JavaScript):

// Equivalent ES5 code
var add = function(a, b) {
  return a + b;
};
console.log(add(2, 3));

Same result, but written in a style that older browsers understand.


⚡ Features Often Transpiled

  • Arrow functions → normal functions
  • let and constvar
  • Classes → function constructors + prototypes
  • Template literals (Hello ${name}) → string concatenation
  • Modules → older script structures

🔄 Transpiling vs. Compiling

  • Compiling: Convert from one language to another (e.g., C++ → machine code).
  • Transpiling: Convert from one version of a language to another (e.g., ES6 → ES5).

So, transpiling is like rephrasing in the same language.


✅ Recap

  • Transpiling = rewriting modern JS into older JS for compatibility.
  • Tools like Babel do the job.
  • Lets developers use new features safely.
  • Output is backward-compatible JavaScript.

✏️ Fill-in-the-Gap Questions

  1. Transpiling converts modern __________ into older versions that browsers can understand.
  2. The most popular JavaScript transpiler is __________.
  3. Arrow functions are often transpiled into normal __________ functions.
  4. The keywords let and const are transpiled into __________ in older JS.
  5. Template literals (Hello ${name}) are transpiled into normal string __________.
  6. Unlike compiling, transpiling converts from one __________ of a language to another.
  7. One advantage of transpiling is that developers can use __________ JavaScript features safely.
  8. A browser that doesn’t support ES6 can still run code if it is __________ first.
  9. Transpiling helps with browser __________ by ensuring code runs everywhere.
  10. The Google project similar to Babel for transpiling is called __________.

<
Previous Post
JavaScript Symbols: Uniqueness Makes Symbols Very Useful
>
Next Post
JavaScript ASI: Imagine Writing Without Full Stops