Why JavaScript

Male computer programmer using laptop at desk in office
Maskot / Getty Images

Not everyone has JavaScript available in their web browser and a number of those who are using browsers where it is available have it turned off. It is therefore necessary that your web page be able to function properly for those people without using any JavaScript at all. Why then would you want to add JavaScript to a web page that already works without it?

Reasons Why You May Want to Use JavaScript

There are several reasons for why you may want to use JavaScript on your web page even though the page is usable without the JavaScript. Most of the reasons relate to providing a friendlier experience for those of your visitors who do have JavaScript enabled. Here are a few examples of proper use of JavaScript to improve your visitor's experience.

JavaScript Is Great for Forms

Where you have forms on your web page that your visitor needs to fill out that form content will need to be validated before it can be processed. You will, of course, have server-side validation that validates the form after it is submitted and which reloads the form highlighting the errors if anything invalid has been entered or mandatory fields are missing. That requires a round trip to the server when the form is submitted to perform the validation and report the errors. We can speed up that process significantly by duplicating that validation using JavaScript and by attaching much of the JavaScript validation to the individual fields. That way the person filling out the form who has JavaScript enabled has immediate feedback if what they enter into a field is invalid instead of their filling out the whole form and submitting it and then having to wait for the next page to load to give them feedback. The form works both with and without JavaScript and provides more immediate feedback when it can.

A Slideshow

A slideshow consists of a number of images. In order for the slideshow to function without JavaScript the next and previous buttons that work the slideshow need to reload the entire web page substituting the new image. This will work but will be slow, particularly if the slideshow is only one small part of the page. We can use JavaScript to load and replace the images in the slideshow without needing to reload the rest of the web page and so make the slideshow operation much faster for those of our visitors with JavaScript enabled.

A "Suckerfish" Menu

A "suckerfish" menu can operate entirely without JavaScript (except in IE6). The menus will open when the mouse hovers over them and close when the mouse is removed. Such opening and closing will be instant with the menu just appearing and disappearing. By adding some JavaScript we can have the menu appear to scroll out when the mouse moves over it and scroll back in when the mouse moves off of it giving a nicer appearance to the menu without affecting the way the menu works.

JavaScript Enhances Your Web page

In all appropriate uses of JavaScript, the purpose of the JavaScript is to enhance the way the web page works and to provide those of your visitors who have JavaScript enabled with a friendlier site than is possible without the JavaScript. By using JavaScript in an appropriate way you encourage those who have a choice as to whether they will allow the JavaScript to run or not to actually have it turned on for your site. Remember that a number of those who do have a choice and who have chosen to turn JavaScript off have done so due to the way in which some sites completely misuse javaScript so as to make their visitor's experience of their site worse rather than better. Don't you be one of those using JavaScript inappropriately and therefore encouraging people to turn off JavaScript.

mla apa chicago
Your Citation
Chapman, Stephen. "Why JavaScript." ThoughtCo, Aug. 27, 2020, thoughtco.com/why-javascript-2037560. Chapman, Stephen. (2020, August 27). Why JavaScript. Retrieved from https://www.thoughtco.com/why-javascript-2037560 Chapman, Stephen. "Why JavaScript." ThoughtCo. https://www.thoughtco.com/why-javascript-2037560 (accessed March 22, 2023).