Compiled Languages Vs Interpreted Languages.

As we saw in the blog post - https://yashpurkar.hashnode.dev/why-do-we-need-languages, a compiler converts high-level, developer-friendly code (which we can write in plain English) into 0s and 1s.

How it works in Compiled Languages (e.g., C++)

  1. Write the Code
// source_code.cpp

#include <iostream> 
using namespace std;

int main() {
    cout << "Hello World" << endl;
    return 0;
}
  1. Compile the Code
xyz g++ source_code.cpp -o executable

This command will compile the C++ source file source_code.cpp into an executable file named executable. The compiler will convert our code into binary so our machine can understand it to execute.

  1. Run the code

     xyz ./executable
     Hello World
    

    Our program's output will be displayed when we run the executable file.

When we have an error in our code
For example, if we do not put a semicolon (;) after endl:

// source_code.cpp

#include <iostream>
using namespace std;

int main() {
    cout << "Hello World" << endl
    return 0;
}

This code will throw a compile-time error:

source_code.cpp: In function 'int main()':
source_code.cpp:6:5: error: expected ';' before 'return'
    return 0;

Until all errors are resolved, the code will not compile, and we cannot see the output of our program.

So, in compiled languages like C++, the process involves writing the code, compiling it to create an executable, and then running the executable to see the output. If there are syntax errors, the compiler will provide error messages, and the code won't compile until all errors are fixed.

How it works in Interpreted Languages (e.g., JavaScript)

  1. Write the code

     // index.js
     const num1 = 10;
     const num2 = 20;
     const result = num1 + num2;
     console.log(result);
    

In interpreted languages, the process is different. It is unlike compiled languages where we: 1. Write code 2. Compile code 3. Run the code.

To see the output of the above code We don't need to compile it (and code is not directly read by a machine It converts into binary but the process is a little different).

  1. Run the code
node index.js
30

We can see the output, so we did not compile the code we directly ran this, but we can still see the output.

Because - Interpreted languages compile line by line.

  1. It will compile the 1st line, convert it into 0s and 1s, and run it.

  2. It will compile the 1st line, convert it into 0s and 1s, and run it.

    And so on...

When we have an error in our code
e.g. We've console the z which is not defined. so it will throw an error

// index.js
const num1 = 10;
const num2 = 20;
const result = num1 + num2;
console.log(result);
console.log(z);

If we run the code our output would be like this:

30
Uncaught ReferenceError: z is not defined

We can see there is an error in our code but it still printed the value of the result variable.

  1. The first line was parsed and executed.

  2. The second line was parsed and executed.

  3. The third line was parsed and executed.

  4. The fourth line was parsed and executed and 30 was printed in the console.

  5. The fifth line was parsed and executed JS engine tried to find z in the memory, but since z was not defined it threw an error. z is not defined

    (Parsing is the process of analyzing the structure and syntax of code to understand its meaning)

So JavaScript is an interpreted language, meaning it is executed line by line. When we run the above code, the first four lines are executed successfully, and 30 is printed to the console. However, when the interpreter reaches the fifth line, it encounters a ReferenceError because z is not defined. Despite this error, the earlier lines have already been executed, and their output (in this case 30) has been printed to the console before the error is encountered.

Interpreter vs Compiler

In simple terms:

Interpreter - When you read the story out loud to your friend and explain each sentence as you go, that's like an interpreter. (You are translating and telling the story directly, sentence by sentence.

Compiler - You converted the whole story into a special picture book that your friend can read on their own without explaining each sentence.