How is JavaScript an interpreted language?


JavaScript is a lightweight and interpreted language, therefore, inside the context of a web browser, you don't even need to buy a compiler. You can start with a simple text editor such as Notepad.

To make our life simpler, various vendors have come up with very nice JavaScript editing tools. Some of them are listed here −

Microsoft FrontPage − Microsoft has developed a popular HTML editor called FrontPage. FrontPage also provides web developers with a number of JavaScript tools to assist in the creation of interactive websites.

Macromedia Dreamweaver MX − Macromedia Dreamweaver MX is a very popular HTML and JavaScript editor in the professional web development crowd. It provides several handy prebuilt JavaScript components, integrates well with databases, and conforms to new standards such as XHTML and XML.

Macromedia HomeSite 5 − HomeSite 5 is a well-liked HTML and JavaScript editor from Macromedia that can manage personal websites effectively.

What is an Interpreted Language?

An interpreted language is one that does not require compiling into machine language. It is executed by an interpreter who reads the source code and converts it into a form that is directly executed. The interpreter executes code line by line which makes JavaScript synchronous in nature.

Languages like C, and C++ need a compiler that converts the programs into a bytecode which is then executed by a machine, compiler executes the complete program at one time, which increases the execution speed whereas JavaScript doesn’t need a compiler; it is directly executed by the browser which interprets the program instruction by instruction. Since the interpreter is executing the program instruction by instruction, this leads the slower execution.

JavaScript Engines in Modern Browsers

Different browsers use different engines to execute JavaScript programs. Google Chrome uses the V8 engine to execute JavaScript Code, whereas Mozilla uses SpiderMonkey, Safari Browser uses JavaScriptCore, and Internet Explorer Browser uses Chakra Engine. So to make sure that a JavaScript program runs exactly the same in all browsers, browsers have to implement Script provided by ECMA International called ECMAScript.

ECMAScript explains how JavaScript should be implemented. In JavaScript, everything is an object, and ECMAScript assumes a "host environment" which is defined as a provider of object definitions.

The latest version of ECMAScript is the ES-13, which is the current version that was released in June 2022.

Each version of ES makes JavaScript, even more, better, and browsers update their engines according to the lasted version of ES so that it is able to run the JavaScript code with the latest feature.

JavaScript is an interpreted language and doesn't require to compile before execution, but V8 compiles JavaScript to native machine code before executing it to increase performance, versus executing bytecode or interpreting it.

Example: Demonstrating JavaScript as an Interpreted Language

Here's an example that demonstrates how JavaScript is an interpreted language −

// This is a simple JavaScript function that calculates the sum of two numbers function sum(a, b) { return a + b; } // We can call the function and print the result to the console console.log(sum(1, 2)); // prints 3 to the console // Now let's modify the function to calculate the product of the two numbers instead function sum(a, b) { return a * b; } // If we run the same code again, we'll see that the function now calculates the product instead of the sum console.log(sum(1, 2)); // prints 2 to the console

In the example above, we define a function called sum() that calculates the sum of two numbers. We then call the function and print the result to the console.

Next, we modify the function to calculate the product of the two numbers instead of the sum. If we rerun the same code, we'll see that the function calculates the product instead of the sum, even though the code has not been compiled or transformed. This demonstrates how JavaScript is an interpreted language since the code is executed directly by the JavaScript engine without compilation.

Conclusion

JavaScript uses an interpreter to execute, which makes it an interpreted language, but the interpreter executes the program's instruction by instruction; this makes the overall execution slow. Since JavaScript executes using an Interpreter, it doesn't require a compiler, this ability lets the JavaScript run on any lightweight application like Notepad. The browsers use different engines to execute JavaScript code, these engines are required to implement the ECMAScript so that all the different browsers will produce the same result.

Updated on: 05-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements