Course Content
Data types and Values
0/1
Object-oriented programming in JavaScript
0/1
Error handling and debugging in JavaScript
0/1
JavaScript functions for string and array manipulation
0/1
JavaScript Libraries and Frameworks
0/1
JavaScript
About Lesson

Program Execution and Interpretation

Have you ever wondered how collections of programming statements communicate instructions to a computer?
This process is known as program execution, which is often referred to as running the program.

Statements like a = b * 2; are useful for developers as they read and write code. However, these statements aren’t directly comprehensible to computers.
To bridge this gap, a specialized tool on the computer, whether an interpreter or a compiler, is employed to translate your written code into machine-readable commands.

For certain programming languages, this translation of commands happens in a sequential manner, from the top of the code to the bottom, line by line, each time the program is executed. This approach is commonly referred to as interpreting the code.

Conversely, in other languages, the translation process occurs beforehand, which is known as compiling the code. When the program runs later, it actually executes pre-compiled machine instructions.

While it’s often stated that JavaScript is interpreted due to processing the JavaScript source code on each run, the reality is more nuanced. The JavaScript engine, in fact, compiles the program on-the-fly and then instantly executes the compiled code.

By definition:

Program execution refers to the process by which a computer runs and executes a program’s instructions. When you run a program, whether it’s a simple script or a complex software application, the computer goes through a series of steps to interpret and perform the tasks defined in the program’s code.

Here’s a breakdown of how program execution works:

# Steps Description
1 Loading The first step is loading the program’s code into memory. This can involve reading the code from a file or receiving it as input.
2 Compilation (if applicable) In compiled languages like C++ or Java, the program code is first compiled into machine-readable binary code before execution. Compilation translates the high-level code into instructions that the computer’s hardware can understand.
3 Interpretation (if applicable) In interpreted languages like Python or JavaScript, the program code is executed line by line by an interpreter. The interpreter reads and executes each statement sequentially.
4 Execution of Statements The computer executes each statement in the order they appear in the code. Each statement instructs the computer to perform a specific action, such as calculations, data manipulation, or control flow operations (if, loops, etc.).
5 Data Manipulation The program can manipulate data stored in variables, arrays, or other data structures. These manipulations might involve performing arithmetic operations, string manipulations, or applying functions to the data.
6 Control Flow As the program executes, control flow statements (if, else, switch, loops) determine which parts of the code should be executed based on certain conditions.
7 Function Calls When a function is called, the program jumps to the function’s code, executes it, and then returns to where the function was called from.
8 Memory Management The computer manages memory usage during execution, allocating memory for variables and data structures and freeing it when it’s no longer needed (garbage collection).
9 Input and Output Programs often interact with the outside world through input (user input, file reading, etc.) and output (displaying information, saving files, etc.).
10 Error Handling If the program encounters errors during execution (syntax errors, logical errors, runtime errors), it may terminate or handle the errors gracefully using error-handling mechanisms.
11 Termination The program continues executing statements until it reaches the end of the code or encounters a specific termination condition. At this point, the program execution completes.
12 Cleanup and Resources After execution, the computer releases any resources (memory, files, network connections) that the program used.