JavaScript is a programming language that runs inside the browser and gives a web page behavior. HTML builds the structure and CSS the look; JavaScript says what should happen when the user clicks a button or types some text. Its name resembles Java, but the two languages are completely separate.
A simple example#
const btn = document.querySelector("button");
btn.addEventListener("click", () => {
alert("Your order has been placed");
});Clicking the button shows a message. In practice, instead of a message it usually sends data to the server or changes a part of the page without reloading it.
Where you run into it#
- Form validation, such as checking a phone number before submitting
- Sliders, dropdown menus and instant search
- Stores that update the shopping cart without refreshing the page
- Modern web apps built with tools such as React or Vue
- On the server side, with the Node.js environment
Important points#
JavaScript is nearly the only language browsers run directly, so learning it is valuable for anyone entering the web. On the other hand, because its code runs in the user's browser, never put a secret key or password in it; anyone can view the code.
A common mistake#
Weighing a page down with too many libraries makes it slow. For a simple job, a few lines of code are usually enough and you do not need to add a large library.