This series of articles will discuss JavaScript and related.

A. Introduction

In modern web development, HTML, CSS, and JavaScrtipt are core technologies for building web pages and applications, where

Why JavaScript?

Because JavaScript is the only scripting language supported by all browsers. For example, VBScript does not support all modern browsers. As a developer, whether you are a C# developer, Java, or C++, you must know more or less about JavaScript because if you develop a Web App, you definitely will use JavaScript.

JavaScrip usually runs in a Browser, but it is not only running in a Browser, JavaScript can be run in a Server without a browser. We will discuss this issue in this article. The content of this article:

B. JavaScript is Considered as a Compiled Language Now [ref]

JavaScript is generally considered an interpreted language, but modern JavaScript engines no longer just interpret JavaScript; they compile it.

This has been happening since 2009 when the SpiderMonkey JavaScript compiler was added to Firefox 3.5, and everyone followed this idea.

JavaScript is internally compiled by V8 with just-in-time (JIT) compilation to speed up the execution.

This might seem counter-intuitive, but since the introduction of Google Maps in 2004, JavaScript has evolved from a language that was generally executing a few dozen lines of code to complete applications with thousands to hundreds of thousands of lines running in the browser.

Our applications can now run for hours inside a browser rather than being just a few form validation rules or simple scripts.

In this new world, compiling JavaScript makes perfect sense because while it might take a little bit more to have the JavaScript ready, once done, it's going to be much more performant than purely interpreted code.

JavaScript can execute not only in the browser but also on the server or on any device that has a special program called the JavaScript engine. The browser has an embedded engine, sometimes called a “JavaScript virtual machine”.

C. Run JavaScript in a Browser

The JavaScript Engine is a program whose responsibility is to execute JavaScript code. All modern browsers come with their own version of the JavaScript Engine, but the most popular one is Google’s V8 Engine. Google’s V8 engine powers Google Chrome browsers.

Here is a list of the different JavaScript Engines for each major Internet browser:

D. Run JavaScript on the Server Side (outside of Browsers)

Is there a way to run JavaScript without a browser, like a shell or batch script? What you are looking for are called JavaScript shells.

A JavaScript shell allows you to quickly test snippets of JavaScript code without having to reload a web page. They are extremely useful for developing and debugging code.

Standalone JavaScript shells

The following JavaScript shells are stand-alone environments, like Perl or Python.

E. What is Node.js (Node)?

The most popular JavaScript runtime outside of a Web Browser is Node.js. We can define Node.js like this (from wiki)

However, it might not be exactly correct because the formal definition on the official Node.js website describes Node.js as

In fact, Node.js can work in both Web Browser and outside of Web Browser.

Node.js (wiki) is a cross-platform, open-source server environment that can run on Windows, Linux, Unix, macOS, and more. Node.js is a back-end JavaScript runtime environment that runs on the V8 JavaScript engine and executes JavaScript code outside a web browser.

Note

  1. Node.js came into existence when its creator Ryan Dahl, understanding the power of V8, powered the Chrome browser and extended it so that it can run on your machine as a standalone application.
  2. V8 is a free and open-source JavaScript and WebAssembly engine developed by the Chromium Project for Chromium and Google Chrome web browsers. The project's creator is Lars Bak. The first version of the V8 engine was released at the same time as the first version of Chrome: 2 September 2008. It has also been used on the server side, for example, in Couchbase, Deno, and Node.js.
  3. Node is sometimes referred to as a programming language or software development framework, but neither is true; it is strictly a JavaScript runtime.

F. Ways to Run JavaScript Outside Browser

Running a JavaScript file in Node.js is straightforward. You need to create a JavaScript file with a .js extension, navigate to the file directory in the terminal, and execute the file with the “node filename.js” command. Additionally, you can pass command-line arguments to your script and use them in your JavaScript code.

We show how to run the JavaScript Code in?

We have a piece of JavaScript code in a file called Test.js.

Test.js

In order to open this JavaScript file, we navigate to the folder from File Explorer, type command: cmd,

CMD

then enter, =>

Command Prompt

Type command in prompt: Code. Then we open a VS Code with the folder opened exactly what we at.

Visual Studio

Type command: Node -v to check if the node.js is installed; we got version: v16.17.0

Version

Run the JavaScript by command: Node Test:

Node Test

We got the result (written into the console) on the same screen as above. We can do the same, either in a VS Code Terminal.

Or in a Windows PowerShell.

Windows PowerShell

References