Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Introduction to JavaScript  ›  Capstone Project

How to Run JavaScript

How to run JavaScript 12 minJavaScript can be run in the browser console for quick tests, through a script tag for web pages, or with Node.js outside the browser.

How to Run JavaScript

In short: there are three everyday places to run JavaScript, and each fits a different job. The browser console runs whatever you type, instantly — for trying an idea. A <script> tag in an HTML page ships code to real visitors, and defer makes sure the page's elements exist before the script looks for them. Node.js runs a .js file with no browser at all, for servers and command-line tools. Wherever it runs, console.log(value) prints to that place's console — never onto the page itself.

Three arrows from "your code" to three boxes: Just me, now → DevTools Console, type it, see it instantly; Page visitors → script src defer, ships with the HTML; No browser at all → node player.js, terminal/laptop/servers

Check a quick result in the console

Every browser's developer tools (usually F12, or right-click → Inspect) has a Console tab that runs whatever you type, right away. Nobody else needs the answer, so there is no reason to build a page for it:

javascript

In the real console you would type 214 / 60 directly and read the answer underneath it. A run block behaves the same way: it runs your code and shows what it prints.

Ship code to page visitors with a script tag

Code that a page's visitors must run every time they load it belongs in a file, loaded with a script tag:

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

defer tells the browser to finish reading the HTML before running the script. Without it, a script in <head> runs before the <body> exists, so document.querySelector("#play") returns null — the element it is looking for has not been read yet. Adding defer, or moving the script to the end of <body>, fixes it. This code needs a real page to run in, so it is shown here rather than run live.

Run a script with no browser at all

A job with no web page involved — renaming files, processing data, running a server — has no DOM to touch, so it runs with Node.js from a terminal instead:

// count.js
console.log("Counting songs in the folder...");
node count.js

Node executes the file directly and prints to the terminal. This is also how the servers behind most web apps are built: the same language, running with no browser underneath it.

Three jobs, three placesStep 1 of 4
  1. Checking a quick result

    You wonder what 214 / 60 gives. Nobody else needs it, so type it in the console: about 3.57.

Quick practice

quick drill

What is the quickest way to check what "4" + 2 produces?

quick drill

Which command runs the file player.js with Node.js?

quick drill

Which attribute of the script tag points to an external JavaScript file?

quick drill

In a web page, where does console.log("hi") show its output?

quick drill

A script in <head> without defer calls document.querySelector("#play") and gets null. Why?

🎯 Your turn

You want to (a) check what 214 % 60 gives, (b) make a playlist page's Shuffle button work for visitors, and (c) run a script on your laptop that renames music files. Explain where you would run the code in each case and why.

Write your explanation and submit it — there's no code to run here, just your reasoning.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPick the place

You want to (a) check what 214 % 60 gives, (b) make a playlist page's Shuffle button work for visitors, and (c) run a script on your laptop that renames music files. Explain where you would run the code in each case and why.