The JavaScript Problem: Mental Model vs Actual Model


“JavaScript is a mix of functional and procedural programming. It is primarily also an event-based language that can provide object-oriented features through prototypical inheritance.”
For a lot of people whose first language is C, and are now learning JavaScript, there seems to exist a mental model that they have of the language, which goes contrary to the actual model. This can be in areas of threading and program execution. Though both languages share a lot in syntactic structure, their operational models are quite different.
Firstly, JavaScript is not a standalone language like C, C++, and Java. For it to run, it needs to be embedded in a container or a host like a browser. Hence, every execution is premised on an action in the host, such as input/output, page loading, and clicks. These operations utilize the functional programming feature of the language — Host actions are wrapped in program-defined functions, and user-defined functions are passed as inputs called callback functions.
Furthermore, JavaScript is an event-based language. This means no code is executed unless an event happens. The program processes are wrapped as event-handlers together with the appropriate event. Also, due to its procedural/sequential feature, every event with its corresponding handler is arranged in a data structure called an Event Queue and is executed one after the other. Unlike C, JavaScript cannot execute multiple operations at the same time; however, it can simulate this process by running some operations in the background while executing other processes in the queue. This is known as Asynchronous JavaScript.
Asynchronous JavaScript is the use of Callbacks, Promises, and Async/Await to simulate multi-threading and write non-blocking programs in JavaScript, which leads to efficient and high-performing web apps.
JavaScript is the language of the web. If there’s a problem with understanding its actual model, a developer may not fully realize the potential of the language. Proper knowledge of how the language works is key to creating products that work.
Subscribe to my newsletter
Read articles from Ugoaneni Victor directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ugoaneni Victor
Ugoaneni Victor
A final year Computer Science Student with strong interest in Computer Networks, Machine Learning, and Language Implementation.