This Is What JavaScript Is Used For

Much of the web is powered by JavaScript

Webpage
Henrik Jonsson / Getty Images

There are a number of different places where JavaScript can be used but the most common place to use it is in a web page. In fact, for most people using JavaScript, in a web page is the only place where they use it.

The Three Languages of a Website

The first requirement of a web page is to define the content of the web page. This is done using a markup language that defines what each of the component parts of the content are. The language that is normally used to markup the content is HTML although XHTML can also be used if you do not require the pages to work in Internet Explorer.

HTML Code
Hamza TArkkol / Getty Images

The HTML defines what the content is. When written properly no attempt is made to define how that content is supposed to look. After all, the content will need to look different depending on what device is being used to access it. Mobile devices generally have smaller screens than computers. Printed copies of the content will have a fixed width and may not require all the navigation be included. For people listening to the page, it will be how the page is read rather than how it looks that needs to be defined.

The appearance of a web page is defined using Cascading Style Sheets that specify which media the specific commands apply to, so the content formats appropriately for the device.

Using just these two languages you can create static web pages that will be accessible regardless of which device is used to access the page. These static pages can interact with your visitor through the use of forms. After a form is filled out and submitted, a request is sent back to the server where a new static web page is constructed and eventually downloaded into the browser.

The big disadvantage of web pages like this is that the only way that your visitor has of interacting with the page is by filling out the form and waiting for a new page to load.

Add JavaScript for Dynamic Pages

JavaScript translates your static page into one that can interact with your visitors without them needing to wait for a new page to load every time they make a request. JavaScript adds behavior to the web page where the page responds to actions without needing to load a new page to process the request.

No longer does your visitor need to fill out an entire form and submit it in order to be told that they made a typo in the first field and need to enter it all again. With JavaScript, you can validate each of the fields as they enter it and provide immediate feedback when they err.

Close up of internet security form
Tetra Images / Getty Images

JavaScript also allows your page to be interactive in other ways that do not involve forms at all. You can add animations into the page that either attract attention to a specific part of the page or which make the page easier to use.You can provide responses within the web page to various actions that your visitor takes so as to avoid the need to load new web pages to respond. You can even have the JavaScript load new images, objects, or scripts into the web page without needing to reload the entire page. There is even a way for JavaScript to pass requests back to the server and handle responses from the server without the need for loading new pages.

Incorporating JavaScript into a web page allows you to improve your visitor's experience by converting it from a static page into one that can interact with them. One important thing to remember though is that not everyone visiting your page will have JavaScript and so your page will still need to work for those who don't have JavaScript. Use JavaScript to make your page work better for those who have it.