Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Your First Program · Lesson 4 of 15

Running JavaScript in the browser

A free lesson from Swipe Coding · JavaScript, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
Time to put your very first line of JavaScript onto an actual webpage. 🚀

Every browser ships with a built-in JavaScript engine, so there's nothing to install to start running JavaScript — you just need a way to get your code onto a page. The most common way is the <script> tag, placed inside an HTML file.

<script>
  console.log('Hello from inline JavaScript!');
</script>
Code written directly between opening and closing <script> tags runs as soon as the browser reaches this point in the page.
🤖
Splitting your JavaScript into its own file keeps things tidy as a project grows. 🗃️

Writing JavaScript directly inside a <script> tag works fine for tiny experiments, but most real projects keep their JavaScript in a separate file (often named something like script.js) and link it into the HTML instead. This keeps HTML and JavaScript organized and lets the same script be reused across multiple pages.

<script src="script.js"></script>
The src attribute points to an external file containing your JavaScript — the browser downloads and runs it in place of this tag.
Fill in the blank

To link an external JavaScript file to an HTML page, you use a <script> tag with a ____ attribute pointing to the file.

Quick check

Why do many developers prefer linking an external .js file instead of writing JavaScript directly inside a <script> tag?

True or false

Every modern web browser has a built-in JavaScript engine, so users don't need to install anything extra to run JavaScript on a webpage.

You'll often see <script> tags placed near the end of the <body>, right before it closes. That way the browser finishes loading the page's visible content first, before it starts downloading and running the JavaScript.

Keep your streak going in Swipe Coding

Swipe through every level of JavaScript on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →