curriculum/challenges/english/blocks/lecture-understanding-asynchronous-programming/673407e02bcf0d682b9a49a9.md
The JavaScript engine has the ability to read, understand, and execute your code. It works like a converter that takes your code, turns it into instructions that the computer can understand and work accordingly.
One of the most well-known JavaScript engines is V8, developed by Google, used in Chrome and Node.js. The JavaScript engine works in a few steps. First, it parses your code, reading it line by line to make sure there’s no mistake in the JavaScript code. Then, it converts this code into bytecode, which is a simpler, intermediate version of your code that’s easier for the computer to understand and execute. Finally, it runs this bytecode to execute your program's instructions. Here's an example of JavaScript code:
const greeting = "Hello, World!";
console.log(greeting);
When you run this code, the JavaScript engine first parses it to check for any syntax errors. Parsing means the engine reads the code and breaks it down into a structure it can understand, checking for mistakes along the way.
Then, it compiles the code into an intermediate format (often bytecode or machine code, depending on the engine). Compiling is the process of converting the human-readable code into a more efficient format that the computer can execute faster.
Finally, the engine executes the code, printing Hello, World! to the console.
Now, let's talk about the JavaScript runtime. The JavaScript runtime is the environment in which your JavaScript code is executed. It includes the JavaScript engine (like V8 in Chrome or SpiderMonkey in Firefox), which processes and executes the code, as well as additional features provided by the environment (such as a web browser or Node.js, which you will learn more about in future lessons).
While the core JavaScript language handles things like variables, loops, and functions, the runtime provides extra tools that allow JavaScript to interact with things outside of the language itself, like the DOM (for web pages) or the Fetch API (for making network requests).
In short, the runtime is what allows JavaScript to do more than just basic programming tasks – like interacting with web pages or handling time-based actions – by providing these extra features beyond the language itself.
While you don't need to know every detail of engines and runtimes to write JavaScript, having a basic understanding can help you write more efficient code and debug problems more effectively.
What is the primary function of a JavaScript engine?
To provide a user interface for writing code.
Think about what needs to happen to your code for it to actually run.
To connect to the internet.
Think about what needs to happen to your code for it to actually run.
To read, interpret, and execute JavaScript code.
To store data in a database.
Think about what needs to happen to your code for it to actually run.
3
Which of the following is NOT typically considered part of the JavaScript runtime?
The JavaScript engine.
Consider what you provide versus what the environment provides.
Web APIs (in a browser environment).
Consider what you provide versus what the environment provides.
The event loop.
Consider what you provide versus what the environment provides.
The JavaScript code you write.
4
Which of the following best describes the difference between a JavaScript engine and its runtime?
The engine provides features like the Fetch API and the DOM; the runtime only parses and executes code.
Think about the distinct roles of the JavaScript engine and the runtime environment.
The engine parses and executes code; the runtime provides the engine plus additional features like the Fetch API and the DOM.
The runtime compiles code into bytecode and machine code; the engine just interprets the code.
Think about the distinct roles of the JavaScript engine and the runtime environment.
They are the same thing—engine and runtime are interchangeable terms.
Think about the distinct roles of the JavaScript engine and the runtime environment.
2