01 / 21
>_

Running JavaScript in the
Browser Console

Learn to write, test, and debug JavaScript code directly inside your browser: no setup, no installs, instant feedback.

JavaScript

What Is the Browser Console?

Every modern web browser (Chrome, Firefox, Edge, Safari) ships with a built-in set of Developer Tools. Inside those tools lives the Console: a live, interactive environment where you can type JavaScript and see the result immediately.

Why Use the Console?

★ Zero Setup
No installs, no accounts. Open any browser and you are ready to code.
★ Instant Feedback
Every expression returns a value immediately: perfect for experimenting.
★ Real Debugging
Inspect variables, test fixes, and debug live websites in production.

Anatomy of Developer Tools

Elements Console Sources Network > console.log("Hello!"); Hello! ← undefined > 2 + 2 ← 4 > document.title ← "My Web Page" > Input prompt Output Return value

How to Open the Console

Browser Windows / Linux macOS
Chrome Ctrl + Shift + J Cmd + Option + J
Firefox Ctrl + Shift + K Cmd + Option + K
Edge F12 → Console tab Cmd + Option + J
Safari N/A Cmd + Option + C

The REPL Model

The console operates as a REPL: a four-step loop that runs every time you press Enter.

Read Evaluate Print Loop parse your input run the code show result wait for input

Console vs. Script File

Feature Console Script File (.js)
Setup needed None Text editor + browser
Execution Line-by-line (REPL) Entire file at once
Code persistence Lost on refresh Saved to disk
Best for Quick tests, debugging Full programs, apps
Multi-line editing Shift + Enter Natural (file editor)

Your First Console Session

1
Open any web page in Chrome (even a blank tab works).
2
Press Ctrl+Shift+J (Windows) or Cmd+Option+J (Mac) to open the Console.
3
Click next to the > prompt and type a simple expression, like 2 + 2.
4
Press Enter. The result appears on the next line.
5
Experiment! Try strings, variables, and console.log() to print messages.

Example 1: Basic Expressions

Console
> 10 + 5 ← 15 > "Hello" + " World" ← "Hello World" > Math.sqrt(144) ← 12 > typeof 42 ← "number"

Example 2: Variables & console.log()

Console
> let name = "Alice"; ← undefined > let age = 16; ← undefined > console.log(name + " is " + age); Alice is 16 ← undefined > age > 18 ← false

Example 3: Interacting with the Page

Console
// Read the page title > document.title ← "Google" // Change the background color > document.body.style.backgroundColor = "navy"; ← "navy" // Count all links on the page > document.querySelectorAll("a").length ← 27

Writing Multi-Line Code

Need more than one line? Hold Shift + Enter to start a new line without executing. The console waits until you press Enter alone to run everything.

Console
> function greet(name) { return "Hello, " + name + "!"; } ← undefined > greet("World") ← "Hello, World!"

Useful Console Methods

console.log()
Print any value to the console. The most common debugging tool.
console.warn()
Prints a yellow warning message. Great for flagging potential issues.
console.error()
Prints a red error message with a stack trace for debugging failures.
console.table()
Displays arrays and objects in a neat, sortable table format.

Console Power Features

1
Autocomplete: Start typing and press Tab to auto-complete property names, methods, and variables.
2
Command history: Press the Up/Down arrow keys to cycle through previously entered commands.
3
clear(): Type clear() or press Ctrl+L to wipe the console output clean.
4
$0 shortcut: After inspecting an element, $0 in the console refers to that selected element.

Common Mistakes

✕ Forgetting Quotes on Strings
Typing console.log(hello) instead of console.log("hello") causes a ReferenceError because the browser looks for a variable named hello.
✕ Re-declaring with let
Using let x = 5 twice in the same console session throws a SyntaxError. Use x = 10 (no let) to reassign, or refresh the page.
✕ Expecting Code to Persist
All variables and functions are lost when you refresh the page. The console is temporary: save important code to a .js file.

Console Cheat Sheet

Action What to Type / Press Result
Open Console Ctrl+Shift+J DevTools Console panel opens
Run expression Type code + Enter Result printed below prompt
New line (no run) Shift + Enter Cursor moves to next line
Previous command Up Arrow Last command reappears
Clear console clear() or Ctrl+L All output removed
Print a value console.log(x) Value of x printed

Question 1 of 5

What does REPL stand for in the context of the browser console?

Question 2 of 5

Which keyboard shortcut opens the Console directly in Chrome on Windows?

Question 3 of 5

You type let x = 10; in the console and press Enter. What value is displayed?

Question 4 of 5

How do you write multiple lines of code in the console without executing after each line?

Question 5 of 5

What happens to your console variables and functions when you refresh the page?