system
Given the following post, respond as if you're commenting on Reddit:
human
Why does a program need to compile?


Why does a program need to be "compiled" after being written, and why (I assume, judging from the Compiling xkcd comic) does it take a long time? I have some very limited experience with Javascript. Why don't Javascript programs need to compile?
gpt
The machine code actually executed by your computer's CPU looks nothing like the source code written by programmers. "Compiling" a program means turning that source code into the corresponding machine code.

Some programming languages aren't "compiled", however; they are instead "interpreted". This is where the programming language has some kind of runtime that decides, based on each line of the program's source code, what to do. Scripting languages like Python and PowerShell are this kind of language. An interpreted program usually runs somewhat slower than a compiled program, since there is a fair bit of overhead in parsing the source code, updating the runtime's state accordingly, and performing the operations the source code describes. On the other hand, a program written in an interpreted languages does not need a potentially time-consuming compilation step before it can be executed. This can mean it's faster for the programmer to get what they want done.

JavaScript is a little bit different from most other scripting languages. JavaScript runtimes typically use what's called "just in time" compilation. You can think of this kind of runtime as dynamically generating and caching the machine code for the program, as and when that machine code is required.

I should also point out that the programming language itself does not really dictate whether it is compiled or interpreted. You could very well use an interpreter for C code, just as you could use a compiler for Python code. But people talk about languages in terms of their most common implementations. The standard implementation of Python is an interpreter (specifically one that converts the source code into a simpler bytecode form, then interprets _that_), so people generally call Python an interpreted language.