
Introduction to JavaScript
What is JavaScript?
JavaScript is a programming language that makes websites interactive. When you click a button and a menu appears, or when you type into a form and see an error message, JavaScript is doing that work. It runs in your web browser —the program you use to view web pages—and responds to what you do.
HTML builds the structure of a web page, CSS controls how it looks, and JavaScript controls what it does. Think of a house: HTML is the walls and roof, CSS is the paint and furniture, and JavaScript is the electricity that makes the lights turn on when you flip a switch.
JavaScript is not the same as Java. They are two different languages with different purposes. The similar names sometimes confuse beginners, but remember: JavaScript was named that way for marketing reasons in the 1990s, not because the languages are related.
Where does JavaScript run?
JavaScript runs in web browsers like Chrome, Firefox, Safari, and Edge. Every modern browser has a JavaScript engine (that is, a program inside the browser that reads and executes JavaScript code).
JavaScript also runs on servers using environments like Node.js. This means you can use JavaScript to build the back-end of a website—the part that handles databases, user accounts, and business logic—not just the front-end that users see. For now, we will focus on JavaScript in the browser, because that is where beginners usually start.
Adding JavaScript to a web page
There are two common ways to add JavaScript to an HTML page.
Inline JavaScript
You can write JavaScript directly inside your HTML file using the <script> tag: