Transpiler vs Compiler

Transpiler vs Compiler

Hey Everyone, I am Bhakti and here we will discuss about Transpiler vs Compiler. So, Let's get started.

To understand a code or list of instructions by any browser or a computer first we need to transform it into some other form, so that instructions get executed. For this purpose we use transpiler and compiler.

What is Transpiler ?

When we write code in some advanced rules (for example ES6), some browser will not be able to understand completely(ES5). So, here comes the transpiler. It converts source code from one programming language to equivalent source code in same or another programming language with similar level of abstraction, so that browser will able to understand. Abstraction means showing only most important or essential data and hiding background details.

It converts the language from one version to another version.

It is also known as source-to-source compilers.

final.drawio.png

for example below is the ES6 code

// ES6 code
let array = [1,2,3,4,5];
const printArray = arr => {
        arr.map(num => console.log(num));
}
printArray(array);

which is converted to ES5 by transpiler. ES6 and ES5 are different versions of the JavaScript language.

// ES5 code 
"use strict";
var array = [1, 2, 3, 4, 5];
var printArray = function printArray(arr) {
        arr.map(function (num) {
                return console.log(num);
        });
};
printArray(array);

some examples of transpiler are :

  • Typescript transpiler : converts Typescript code to JavaScript

  • Emscripten : converts C/C++ to JavaScript

  • Babel : converts ES6+ code to ES5

What is Compiler ?

Computers can't understand the human readable format, it only understands 0 and 1. so compiler is the intermediate here, which converts the source file in human readable format to an machine understandable object file without changing the meaning. That means it converts high-level language to low-level language. C, C++ and java languages use compilers. Example of compilers are gcc, clang, javac, go.

compiler.drawio.png

So, Transpiler converts one programming language to same or another programming language ( or one version to another version) and Compiler converts High level language to low level language that is, machine language. This is the basic difference between them.

I hope you understand the difference between Transpiler and Compiler, Thanks for reading.