Running JavaScript in the Console and in a Script File
Open the browser console, run your first lines there, then move the same code into a .js file linked from an HTML page. After this, you have a setup that runs every example in this course.
Course content
Sign in to save your progress.
-
Before You Write Code
-
What JavaScript Actually Does in Your Browser
-
Running JavaScript in the Console and in a Script File
-
-
-
The Basics You Cannot Skip
-
JavaScript let, const, and Why Not var
-
JavaScript Data Types and typeof
-
JavaScript == vs ===: Comparing Values Without Surprises
-
-
-
Making Decisions and Repeating Work
-
JavaScript if, else and the Ternary Operator
-
JavaScript Loops: for, for...of and while
-
-
-
Organizing Your Code
-
JavaScript Functions and Arrow Functions
-
JavaScript Arrays and Objects
-
-
-
Touching the Page
-
Changing the Page With the JavaScript DOM
-
JavaScript Click and Form Events
-
Reading JavaScript Errors in the Browser Console
-
-
- Final Check: JavaScript for Beginners
Skip ahead: If you can already open the console with F12 and you have an HTML page loading a .js file, skip to "Common mistakes" and check you're not making one of them.
You don't need to install anything to run JavaScript. Your browser already has it. What you need is a place to type code and see the result, and a way to save that code so you're not retyping it. The console gives you the first. A script file gives you the second.
First, check what you have
- A browser: Chrome or Edge is easiest to follow here. Firefox is fine.
- A text editor. Notepad works, but a code editor like Visual Studio Code is free and shows mistakes in colour. Use it if you can.
- A folder for this course, for example
js-courseon your Desktop or in Documents.
That's it. No Node.js yet. That comes in JavaScript Intermediate.
Step 1: Open the console
Open any web page, even a blank tab. Then:
- Windows and Linux: press
F12orCtrl+Shift+J(Chrome, Edge). - Mac: press
Cmd+Option+J. - Or right-click the page, choose Inspect, then click the Console tab.
You'll see a panel with a > prompt. That's where you type.
⚠ Warning: If you see a message telling you to paste code into the console to "unlock" something or "verify your account", don't. That's a common scam. Code pasted into the console runs with your login on that site and can read or send things as you. Only run code you wrote or understand.
Step 2: Run your first lines
Type this and press Enter after each line:
console.log("Hello from the console");
2 + 3 * 4;
Hello from the console
The console shows Hello from the console, then 14 for the second line. The console prints the value of the last expression you typed, so 2 + 3 * 4 shows 14 even without console.log. In a script file, that doesn't happen. Only console.log prints. Remember that, because it confuses people when they move code from the console into a file.
You might also see undefined after a line. That's the console telling you the line itself didn't produce a value. console.log prints something but returns nothing. It's not an error.
Step 3: Move the code into files
The console forgets everything when you reload the page. For anything longer than a few lines, use files. In your course folder, create two files.
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JS practice</title>
</head>
<body>
<h1>JS practice</h1>
<script src="script.js"></script>
</body>
</html>
script.js:
const name = "Ana";
console.log("Hello, " + name);
console.log("script.js is loaded");
Hello, Ana
script.js is loaded
Now double-click index.html to open it in your browser, then open the console. You should see both lines. If you change script.js, save it and reload the page (F5 or Cmd+R). The page only runs the new code after a reload.
Why the script tag goes at the bottom
Put the <script> tag just before </body>, like above. The browser reads the page from top to bottom. If the script runs before the button or paragraph it wants to change exists, it can't find it and you'll get an error like Cannot read properties of null. Putting it at the bottom means the page is already there. You'll see a cleaner way to handle this (defer) later, but bottom-of-body is the simple, safe default.
💡 Tip: Keep the console open while you work. Errors show up there in red with a file name and line number, and clicking the file name takes you to the exact line. The last lesson of this course goes through reading them.
Common mistakes
- Saving the file as
script.js.txt. Windows hides extensions by default, and Notepad likes to add.txt. Turn on "File name extensions" in File Explorer's View menu and check. - Wrong file name or folder in
src.src="script.js"means "a file called script.js next to this HTML file". If the console says404orERR_FILE_NOT_FOUNDfor script.js, the name or folder doesn't match. - Editing the file and not reloading. The browser doesn't watch your files. Save, then reload.
- Typing code in the address bar. That's not the console. Use F12.
What happens if you skip this
You'll end up running everything in online playgrounds that hide the parts you need to understand: where the file lives, how the page loads it, where errors appear. That works until your first real project, where nobody set it up for you.
Before the next lesson, try this: change the name in script.js to your own, save, and look at the console without reloading. What does it show, and why?
About
Open the browser console, run your first lines there, then move the same code into a .js file linked from an HTML page. After this, you have a setup that runs every example in this course.
- Duration
- 5 min
- Last update
- 08/27/2026
Comments (0)
Sign in to commentNo comments yet.