No history yet

Introduction to JavaScript

What Is JavaScript?

Think of a website as a person. HTML provides the basic structure, like a skeleton. CSS adds the styling—the clothes, hair, and eye color. But JavaScript? That's what brings the person to life. It's the brain and muscles, allowing the website to react, move, and interact with you.

JavaScript (often shortened to JS) is a programming language that runs directly in your web browser. You don't need to install any special software to use it. When you visit a website, your browser's engine reads the HTML for content, the CSS for style, and then executes the JavaScript code to make the page interactive.

Making Web Pages Interactive

What does "interactive" mean? It means the page can do things without needing to reload from a server. JavaScript is responsible for almost everything that changes on a webpage after it first loads.

This includes things like:

  • Showing or hiding elements when you click a button.
  • Validating forms to make sure you've entered a proper email address before you hit submit.
  • Displaying animations, sliders, and pop-up alerts.
  • Loading new data, like the latest posts on a social media feed, as you scroll.
Lesson image

Without JavaScript, the web would be a much more static, boring place. It would be like a library of linked documents instead of the collection of dynamic applications we use every day.

Your First JavaScript Program

Let's write some code. The great thing about JavaScript is that you already have everything you need to get started: a text editor and a web browser.

While you can use any plain text editor (like Notepad on Windows or TextEdit on Mac), most developers use a dedicated code editor. A great, free option is Visual Studio Code.

We'll start with the classic "Hello, World!" program. The goal is to make this message appear in the browser's developer console, a tool we'll use a lot to test our code.

First, create a new file and name it index.html. This file will contain our HTML structure and the JavaScript code. Copy the following into your file:

<!DOCTYPE html>
<html>
<head>
  <title>My First JS Page</title>
</head>
<body>

  <h1>Welcome to JavaScript</h1>

  <script>
    // This is a JavaScript comment
    console.log("Hello, World!");
  </script>

</body>
</html>

Save the file and open it in any modern web browser like Chrome or Firefox. You'll see the heading "Welcome to JavaScript", but where is our "Hello, World!" message?

It's in the developer console. To see it, right-click on the page, select "Inspect," and then click on the "Console" tab. You should see your message printed there.

Two Ways to Add JavaScript

In the example above, we put our JavaScript directly inside the HTML file using <script> tags. This is called internal JavaScript. It's fine for small, simple scripts.

When projects get bigger, it's best to keep your JavaScript in separate files. This makes your code cleaner, more organized, and easier to reuse across different pages.

This is called external JavaScript. To do this, you would create a new file and save it as script.js. The .js extension tells the browser it's a JavaScript file. Inside script.js, you'd only have the JavaScript code:

// script.js
console.log("Hello from an external file!");

Then, you would update your index.html file to link to this new file. Notice the src attribute in the <script> tag, which points to the location of your file. It's common practice to put script tags right before the closing </body> tag.

<!DOCTYPE html>
<html>
<head>
  <title>My First JS Page</title>
</head>
<body>

  <h1>Welcome to JavaScript</h1>

  <script src="script.js"></script>

</body>
</html>

Now if you open index.html in your browser and check the console, you will see the message "Hello from an external file!". You're officially running JavaScript!

Quiz Questions 1/5

Using the analogy of a website as a person, where HTML is the skeleton and CSS is the clothing, what does JavaScript represent?

Quiz Questions 2/5

To view the output of a console.log('message') command, where should you look in your web browser?