Updated June 12, 2023
Introduction to How JavaScript Works?
JavaScript is a scripting language that is used within the browser for interface interaction. Brendan Eich was the first person to create JavaScript engine was for the Netspace Navigator web browser at Netspace. It is implemented in C and its code name was SpiderMonkey. Mocha was the name given to the project initially, then renamed to LiveScript and finally when Netspace and Sun did license agreement it was again renamed to JavaScript. In this topic, we are going to learn about how javascript works. We must have heard about popular engines like V8, Spider Monkey and there are many others. Different engines have different “codenames”, for example:
- V8 – engine used in Opera & Chrome.
- SpiderMonkey – engine used in Firefox.
- Trident, Chakra – engine used in IE & Nitro.
- ChakraCore – engine used in Microsoft Edge.
- SquirrelFish – engine used in Safari.
The engine used for javascript consists of two main components:
- Heap Memory— this is where the allocation of memory happens.
- Call Stack — this is the place where the stacks are getting called and the code executes.
NOTE:
JavaScript, a single-threaded programming language, which means it has a single Call Stack and thus it has the ability to do one thing at a time. The Call Stack is basically a data structure that records the point in the program which is actually executing. This call stack is similar to other stacks having the basic functionality like, if we step into a function, we put it on the top of the stack. We have to pop off the top of the stack if we want to return from a function. This is the basic functionality of every stack. As we are discussing the engines which drive the JavaScript functions are really complicated but the basic things which every engine performs in the broader sense are as follows:
- If the browser reads the script then the engine is embedded in the browser itself.
- After reading the script it converts (“compiles”) the script to the machine language.
- And then the machine code runs, pretty fast.
What can in-browser JavaScript works do?
- It changes the existing content, modifies styles by adding new HTML to the pages.
- Provides functions like a mouse click, mouse hover, etc. which reacts to the actions of the use.
- Send and Receive requests over the network to remote servers, without loading the complete page as the user inputs. One such technology which is very famous is AJAX.
- With this one can set and get the values of cookies, provides messages to the users for information, alert and other purposes.
- Also can store temporary data on client-side.
What CAN’T in-browser JavaScript works do?
JavaScript’s abilities are somewhat limited within the browser are limited for the sake of the user’s safety. The aim is to prevent some third-party webpage from accessing private information or harming the user’s data. Some scenarios for this kind of limited access are:
- By default, JavaScript on a webpage is restricted to read/write arbitrary files on the hard disk, copy them or execute programs. It even has no direct access to OS system functions. Few modern browsers also allow it to work with files, but for the same, the access is limited and only provided if the user does certain actions, like “dropping” a file into a browser window. Some of the ways are also available to interact with camera/microphone and other devices, but that also requires user’s explicit permission. So a JavaScript-enabled page may not secretly enable a web camera or may send the information to the remote server or any third party.
- Different tabs or windows generally do not know about each other instances. But technically that is very possible. The same is called “Same Origin Policy”. For the same to work, both instances must contain some special JavaScript code that can handle data exchange between each other. But the limitation is again there for user’s safety. For example, a web page from URL abc.com which a user has opened must not be able to access another browser tab with the URL xyz.com and steal information secretly.
- With the use of it, one can easily communicate over the net to any remote server or third party where the current page came from. But for security reasons, its ability to receive or send data from other sites or domains is disabled. But the same is still possible, for which some JavaScript works require explicit agreement from the remote side. Such limitation will not be possible to provide, if JavaScript would be used outside of the browser rather than inside, i.e. either from a server. You may also notice that some of the modern-day browsers allow installing plugin or extensions by which JavaScript works used inside the browser to get extended permissions.
What makes JavaScript work unique?
- As we all know it is an object-based scripting language.
- It is written in C. It is an interpreted language.
- Pure JavaScript is faster in accessing DOM.
- With the use of JavaScript work, the user gets more control over the browser.
- It can also handle date and timestamps.
- It can also detect the user’s browser and even OS.
- It is lightweight.
- It is a scripting language as already stated.
- It is an interpreter-based scripting language.
- It is case sensitive i.e. syntax is case sensitive.
- It is an object-based language as it provides predefined objects like Java but it is not java.
- Each and every statement in JavaScript must be terminated with a semicolon (;).
- The most important thing which JavaScript provides is the ability to create new functions within scripts. One can declare a function in JavaScript using the function
- Full support for HTML and CSS.
- It is very easy to use.
- Supports for all browsers.
Limitations
Among the many advantages JavaScript has some limitations also which are given below:
- JavaScript is on the client-side and it does not provide the facility to read or write of files on disk.
- JavaScript cannot be used for networking applications because it does not provide any such kind of support.
- It does not provide any functionality like multithreading or multiprocessor.
Conclusion
So with that, we all came to know that about some basic information about the evolutions of JavaScript works, some of the famous engines which run JavaScript on web browsers, what are the advantages of keeping JavaScript inside the browsers, and what could happen if JavaScript could be placed and run outside the browsers. We have also come across some of the features of JavaScript work which made it usable and unique along with some of the limitations/disadvantages of it. So with that, I am concluding the topic on how javascript works, there are many more things to explore which you could explore from the web.
Recommended Articles
This has been a guide to JavaScript work. Here we have discussed the basic concepts, limitations, and what makes javascript unique from others. You may also look at the following articles to learn more –