Mostly because you want the website you’re visiting to work as intended.
Well, the short answer is that JavaScript is a programming language, and you need it because the website says you do.
I know, that’s not very helpful. Let’s see if I can explain in a little more detail.
What is JavaScript
JavaScript is a programming language that can be used on webpages to implement anything from simple interactivity to complex computer programs that run within your browser. Some websites rely on JavaScript to be able to perform their tasks.
Programs do everything
Anything that happens on your computer happens because some kind of program tells your computer to do it. In fact, quite often it’s some kind of program telling some other kind of program to do it. It can get quite complicated, as you can imagine.
Take webpages, for example. At its core, each webpage is a kind of computer program that tells your browser what to display and how to display it.
Programming languages
Computers don’t understand English. Besides, English is too ambiguous for the precision required to properly program a computer. Computers only work in the absolute.
There are hundreds if not thousands of different computer programming languages. Each has its own pros and cons, strengths and weakness, and abilities and purposes.
There are several different languages that might be used to have a browser display a webpage: four, in fact. Let’s meet them.
Languages and webpages
In very over-simplified terms, there are four languages that come into play when a webpage — including the page you’re reading now — is displayed.
HTML
HyperText Markup Language is the basic language of the web. It describes how a page should look.
When you request a webpage, a page full of HTML is delivered to your browser by the web server. The browser follows those HTML instructions to present the page. Those instructions include what fonts to use, how big to make them, whether to make some words bold or italic, where to place an image, how to format a table of information, and much more.
The limitation of HTML is that it’s static. Once the page is displayed, it’s done.
CSS
Cascading Style Sheets are an augmentation of HTML that separate style and appearance from actual content.
A good way to think of it is that HTML will say “Here’s a paragraph, and here’s what it contains”, whereas CSS might say “All paragraphs use this font at this size with these colors” and so on.
The line between CSS and HTML is blurry, since much of what CSS controls can be included directly in HTML. But the ideal is that when your browser requests a webpage:
- A page of HTML is downloaded.
- One of the early instructions in the HTML is to download another file, called a style sheet, containing all the CSS for that page.
When implemented cleanly,1 simply replacing one CSS style sheet with another would completely change the appearance of a page.
DHTML
Dynamic HTML adds more programming constructs to HTML to create a way for the DHTML “program” to “do” things dynamically.
For example, with DHTML, a webpage can specify actions to be taken when certain events happen — for example, to change the color or appearance of some text when the mouse pointer hovers over it.2
JavaScript
JavaScript isn’t specifically related to HTML — it can be used elsewhere — but interacts with HTML and DHTML when used in a browser.
It’s a more traditional programming language in that you can write a series of instructions to compute what kinds of actions should be taken based on various conditions, repeat things a variable number of times, and just generally take more complex and sophisticated actions. JavaScript programs, when used on webpages, run entirely within your web browser. Entire applications and games have been written in JavaScript.
The line between DHTML and JavaScript can get blurry, but a rule of thumb I use is simple: DHTML is about the webpage, period. The items on a webpage, the events that a webpage might want to act on (like moving your mouse over text), and the resulting changes to the webpage that you might want to have happen resulting from those events (like changing the color of text).
JavaScript, however, is really independent of the web and HTML. It adds programming constructs like loops, variables, and functions. You could write a small program to calculate Pi in JavaScript, and then perhaps use DHTML to do something interesting on the webpage with the results.
Do this
HTML, DHTML and JavaScript are all programming languages used to make your webpages look and act the way they do.
While HTML and DHTML are fundamental parts of your browser, for various reasons JavaScript can often be turned off. There are even browser extensions that will do so on a site-by-site basis for security.
But that doesn’t stop some sites from requiring it. They’ve written portions of their interface using JavaScript in order to provide their functionality. So in order to use that particular website, you must have JavaScript enabled.
So the bottom line is that you need JavaScript because the websites you’re visiting have chosen to use it. There’s no alternative other than not using that site.
One good reason for enabling Javascript is that, by and large, Twine won’t function without it.
Yeah, I know — esoteric. But true just the same.
(For those who don’t know, Twine is one programming language — specially developed for interactivity (this often, but not always, means “CYOA-type” games) — that makes use of yet another programming language (Javascript in this case) in order to function.
“All paragraphs use this font at this size with these colors.” Wouldn’t it be more accurate to say, “All paragraphs of this paragraph type use this font at this size with these colors”? Similar to how paragraph styles work in word processing programs.
The good news: all common browsers have JavaScript enabled by default. The bad news: Occasionally some websites don’t work on all browsers because not all browsers handle all JavaScript commands the same: This doesn’t seem to be as much of a problem as in the old days, as browsers have it more together now. My guess is that since most browsers are based on Chromium (Chrome), they are more consistent. Firefox is the outlier, but most websites are tested on Firefox as well as a few others.