DEV Community

Cover image for Introduction To JavaScript
enigma6174
enigma6174

Posted on • Originally published at dev.to

Introduction To JavaScript

JavaScript is a dynamic, weakly typed programming language which is compiled at runtime. It can be executed directly as a part of a webpage in a browser or on any other machine i.e. host environment.

JavaScript was created to make webpages more dynamic (e.g. change the content of the website directly from inside the browser). Originally, it was called LiveScript but due to the popularity of Java it was later renamed to JavaScript

Note: JavaScript is totally independent from Java and has nothing in common!

How Do Webpages Work?

When the user visits a webpage on their browser (for e.g. Google) what they are doing is connecting to the Google servers and asking them for the Google homepage. This is called as making a request. The Google server then validates this request and returns the requested file (the Google homepage in our case). This is called a response.

Now, let us consider that the user makes another request - maybe clicking on some search settings which displays a popup window with advanced options. The user can select or unselect checkboxes, radio buttons, select items from a list etc. Now, all these features are made possible by JavaScript.

js-intro

How JavaScript Gets Executed

When a JavaScript code is run (and let us assume, for simplicity that the effects will be seen on the website) then, before being executed the JavaScript code actually passes through something called as a JavaScript Engine. This is in built into the browser and each browser comes with it's own implementation of the JavaScript Engine. For example, the JS engine built into Chrome is called V8 and the one inside Firefox is called SpiderMonkey.

The JavaScript Engine executes the JS code in 3 steps - Parsing, Compiling, Execution

Parse

In this phase the JS Engine reads the code, understands the code (i.e. what are the variables, what are the functions etc.) and performs some pre execution steps (like removing unwanted whitespaces, moving function defintions etc.)

Compile

In this phase, the parsed code is converted to machine code (i.e. a format understandable by the browser or the host machine). Conversion to machine code happens on-the-fly which means that the code conversion happens at source line by line.

Execute

In this phase, the machine code is then executed line by line and the results can be seen on the website or the host machine.

js-execute

JavaScript Is Dynamic And Weakly Typed

Dynamic means that JavaScript is not pre-compiled and instead parsed and compiled on-the-fly. In programming languages like Java, C++ etc. the entire code first needs to be compiled into an intermediate file and then, this intermediate file is converted into an executable file when required.

Note: In dynamic programming languages, the written code is directly converted to executable without going through the intermediate phase. Therefore, there is no need to compile the code first. The entire compilation and execution process happens together at runtime.

Weakly Typed means that the type of the data can change at runtime and datatypes are assumed automatically. This means that we do not have to specify the type of data a variable will hold. Whatever type of value is assigned to a variable, becomes the type. For e.g. a variable can hold numbers at one point (Number type) and later on that variable may hold string (String type)

javascript-dynamic-weak

JavaScript Runs On A Host Environment

Host environment refers to the system or the machine that is executing the JavaScript code. JavaScript code cannot run standalone, it always needs an environment to run.

The most common envrionment to run JavaScript code is inside the browser. When running in a browser, JavaScript is used to make the websites more dynamic. It can manipulate HTML and CSS and also send and recieve HTTP requests among other things.

Note: Inside the browser JavaScript cannot access the local filesystem, interact with the host OS etc.

JavaScript can also run on the server side or a remote machine. This is basically the extracted JavaScript Engine running inside a C++ module. It can be executed on any machine and is therefore used extensively to design the backend web servers, API services etc.

Note: Server side JavaScript can access the local filesystem, interact with host OS etc. It however cannot manipulate HTML or CSS

javascript-summary

This completes the introduction to JavaScript!

If you liked this article and want to stay updated with more such articles, support me by following me on DEV and Twitter

I write about web development, AI, cloud, entrepreneurship, technology and anything else that excites me!

Discussion (0)