JavaScript Journey | Part 1
13 February 2023
Bibhabendu
Let's have a closer look about javascript 2023
Javascript
Let's have closer look at JS
- History of JavaScript
- JavaScript Use cases
- Why Browser important
- How JavaScript Run
History Of JavaScript :
JavaScript was invented by Brendan Eich in 1995 while he was working at Netscape Communications Corporation. Eich was tasked with creating a scripting language for the Netscape Navigator web browser that would allow developers to create interactive web pages.
The initial version of JavaScript was called Mocha, and it was later renamed to LiveScript. However, due to a marketing agreement with Sun Microsystems, it was eventually renamed to JavaScript.
JavaScript Use cases :
JavaScript is a versatile programming language that can be used in a wide variety of contexts and applications. JavaScript is like BOSS!
- Web development: JavaScript is a key technology for building dynamic, interactive web applications. It is used to create animations, handle user interactions, and manipulate the content of web pages in real time.
- Mobile app development: JavaScript is also used in mobile app development that is powered by react-native. React-native is a
cross-platform (code one run anywhere) mobile app development technology based on javascript - Game development: JavaScript is increasingly used in game development, both for browser-based games and for more advanced games
- Internet of Things (IoT): JavaScript can also be used in IoT applications, example, to control sensors and actuators, collect data from IoT devices, and connect to cloud services.
Why Browser Important :
Till this point we know Who invent JavaScript And what are the application of JavaScript in various field.
But now the question is How JavaScript run ?. If you guys have some idea about how coding works (from 1st Year btech) then we often notice this term called COMPILER.
Any code that we try to run on our system example : take a C code and run. For that we need a C compiler that convert our High level code (Human readable) to machine code (machine readable) then we get the output.This is the simple job of compiler.
Then the answer of the above question "How JavaScript run" is by using
Compiler - The is Correct Let's see --
When JavaScript was first released, it was initially designed to be executed in the browser, where it was interpreted by the browser's JavaScript engine. Look at the term JavaScript engine.
A JavaScript engine is a browser program that interprets and executes JavaScript code. It converts JavaScript code into machine code that can be run by a computer's processor. The engine provides the runtime environment for JavaScript code.
How JavaScript Run :
When you write any JavaScript code and feed it to the browser or Website loading as real like example The JavaScript Engine come to
rescue and to the following steps:
- Lexical analysis: The JavaScript code is analyzed to break it up into a series of tokens, which represent the basic elements of the code such as keywords, identifiers, and literals.
- Parsing: The tokens are parsed to create a parse tree, which represents the structure of the code.
- AST optimization: The parse tree is optimized to create an Abstract Syntax Tree (AST), which is a more efficient representation of the code.
- Bytecode generation: The AST is then translated into bytecode, which is a low-level representation of the code that is optimized for execution by the JavaScript engine.
- JIT compilation: Some JavaScript engines, such as Google's V8 engine, use Just-In-Time (JIT) compilation to further optimize the code. This involves dynamically translating the bytecode into machine code as the code is executed, which can result in significant performance improvements.
- Machine code generation: Finally, the bytecode or JIT-compiled code is translated into machine code that is specific to the CPU architecture on which the JavaScript engine is running.
When the all above steps are completed we get the optimized machine code that run on the cpu and we can get the output.
Some question may in you mind:
Question 1
We know that the Higher Level JavaScript code is converted to bytecode [High Level JS code] ---> [intermediate ByteCode] ---> [Machine code]
Don't you think that it take some bit extra time Then why ByteCode is
needed?
Ans: -- Yes It take some extra time compared to simply compiling JavaScript code to machine code.However, there are several reasons why bytecode is used in JavaScript engines:
- Platform independence: Bytecode allows JavaScript engines to be more portable across different platforms and architectures, as the bytecode can be generated once and executed on any platform that has a compatible bytecode interpreter.
- Optimization: Bytecode can be optimized to improve the performance of the JavaScript code, as the interpreter can perform optimizations such as inlining and dead code elimination during bytecode execution.
- Security: Bytecode can be used to provide an additional layer security, as the bytecode can be obfuscated or encrypted to make it harder to reverse-engineer or tamper with.
Question 2
Can i say that JIT compiler will convert JS code to directly machine code and skip the byte code generation steps:
Ans:-- Yes, you are correct. JIT (Just-In-Time) compilers can skip the
bytecode generation step by compiling JavaScript code
directly to machine code.
JIT compilers work by analyzing the JavaScript code as it is
being compiling into machine code on the fly. This approach
can lead to faster execution times than using a bytecode
interpreter
However, not all JavaScript engines use bytecode. Some
engines, such as V8, use JIT compilation exclusively, while
others, such as SpiderMonkey, use a combination of bytecode
and JIT compilation. The choice of bytecode vs JIT compilation
depends on various factors, including performance, memory
usage, and platform compatibility.