Mon Sep 09 2024
~ 4 mins
JavaScript is a powerful and versatile programming language, widely used in web development and beyond. In this article we'll be going over the key details of javascript and how it runs in your computer
We should always remember that the a programming language is just a way of communicating with and controlling a computers hardware.
Whenever a piece of code is executing it's using your computer's RAM, CPU, etc to save variables and run computations
Javascript is a high-level programming language which means that you don't manually have to worry about details like allocating memory for variables. This is all handled in the background for your.
This allows programmers to focus on solving their specific problems instead of worrying about the intricate details of using a computer's hardware.
In lower-level languages like C, managing memory is a manual process. Every time you create a variable or object, you must allocate memory for it. Once you're done using it, you must manually deallocate that memory.
If you forget to do this, it can lead to memory leaks, where unused memory accumulates and can eventually cause your application to crash or slow down.
JavaScript is a high level language so it automatically identifies and frees up memory that is no longer in use by the program. This process is called garbage collection.
JavaScript is a single-threaded language, meaning it has access to only one CPU thread and can execute only one task at a time. This characteristic can be both an advantage and a limitation.
On the one hand, it simplifies the execution model, making it easier to write and debug code. On the other hand, it means that if a single task takes a long time to complete, it could block other tasks from running, causing delays in your application.
Whenever you run JavaScript code, it’s executed within a JavaScript runtime environment. This environment is an area of your computer that has everything it needs to read and execute your js code.
It has access to your computer's hardware and software resources, and it is made up of components that work together to execute your code efficiently.
A typical JavaScript runtime consists of several key components:
These components work together to ensure that your JavaScript code runs smoothly and efficiently.
The JavaScript engine is the core component of the runtime responsible for executing your code.
When you load a web page in a browser, the browser's JavaScript engine reads the HTML document and sends any js code it encounters to the js engine.
These engines parse your JavaScript code, compile it into machine code (a language the computer's hardware can understand), and execute it.
One of the reasons JavaScript is so powerful is because of the huge number of libraries and APIs (Application Programming Interfaces) that are available inside the runtime environment.
These libraries and APIs add additional functionality that is not part of the core JavaScript language but can be used by your code.
These libraries allow you perform tasks like making network requests, manipulating the DOM (Document Object Model), and more.
For instance, when you use the fetch function to make an HTTP request, you’re actually using a feature provided by the browser's API, not the JavaScript language itself.
When you call fetch, the JavaScript engine hands off the task to the browser's API. This API has access to it's own CPU thread so can execute the task while engine to continue executing other tasks.
Once the API has finished processing the request, it adds any associated callback functions (functions that should run once the task is complete) to a queue. The JavaScript engine then picks up these callbacks from the queue and executes them, ensuring that your code continues to run smoothly.
The event loop is a crucial component of the JavaScript runtime, responsible for managing the execution of asynchronous tasks.
It constantly monitors the call stack (where the current code being executed is stored) and the callback queue (where completed asynchronous tasks are stored).
When the JavaScript engine is free (i.e., the call stack is empty), the event loop picks the first task from the callback queue and sends it to the engine for execution.
This process ensures that asynchronous tasks, such as network requests or timers, do not block the execution of other code, allowing js to manage multiple tasks efficiently.
In summary, JavaScript is a high-level, garbage-collected language designed to be easy to learn and use.
It runs within a JavaScript runtime environment, which provides all the necessary components—such as the JavaScript engine, libraries and APIs, and the event loop—to execute your code efficiently.
JavaScript's single-threaded nature is managed by the event loop, allowing the language to handle asynchronous tasks without blocking the execution of other code.
Understanding these fundamental aspects of JavaScript will help you write more efficient, effective code and give you a deeper insight into how your JavaScript applications operate behind the scenes.