How to Execute JavaScript in Chrome DevTools
Master Chrome DevTools to write, run, and debug JavaScript directly in the browser. Learn the Console, Sources panel, Snippets, and breakpoints.
This guide shows you how to execute JavaScript in Chrome DevTools, the set of tools built into every Chrome browser. For JavaScript developers, it is the single most important tool after a code editor. It lets you write code, see output, inspect errors, and debug problems, all inside the browser.
Opening DevTools
There are several ways to open DevTools:
- Press
Ctrl+Shift+I(Windows/Linux) orCmd+Option+I(Mac) - Right-click anywhere on a page and choose Inspect
- Press
F12
The DevTools panel opens at the bottom or side of your browser window. You can drag the divider to resize it, or click the three-dot menu in the top-right of DevTools to change the dock position.
The Console Panel
The Console is where you write JavaScript and see results immediately. It is the fastest feedback loop in programming.
Running Code
Type any JavaScript and press Enter:
const name = "Chrome";
console.log(`Hello from ${name}!`);The Console runs both lines immediately and prints whatever console.log() was given, right below the code you just typed and ran:
Hello from Chrome!The Console evaluates each line and prints the return value. If a line has no explicit return, it prints undefined.
Console Methods Beyond log()
console.log() is the most common method, but several others are more useful in specific situations:
const users = [
{ name: "Alice", role: "admin" },
{ name: "Bob", role: "editor" },
{ name: "Charlie", role: "viewer" },
];
// console.table() displays arrays and objects as formatted tables
console.table(users);This prints a sortable table with columns for name and role, much easier to scan than nested object output. Other console methods are useful for different situations, like flagging problems or grouping related output together:
// console.error() prints in red with a stack trace
console.error("Something went wrong!");
// console.warn() prints in yellow
console.warn("This is a warning.");
// console.group() organizes related logs
console.group("User Details");
console.log("Name: Alice");
console.log("Role: admin");
console.groupEnd();Accessing the Current Page
The Console is not limited to standalone code. It can also read and change the actual page you are viewing. Try running these three lines on any website:
// Read the page title
console.log(document.title);
// Count all links on the page
const links = document.querySelectorAll("a");
console.log(`This page has ${links.length} links.`);
// Change the background color temporarily
document.body.style.backgroundColor = "#f0f0f0";The last line changes the page background for your current session only. Refresh the page and it reverts. This is invaluable for testing CSS changes and understanding how a site works.
The Sources Panel and Snippets
The Console does not save your code. The Sources panel solves this with Snippets, JavaScript files that persist in your browser.
Creating a Snippet
Open the Sources tab
Open DevTools and click Sources.
Find the Snippets section
Look in the left sidebar. You may need to click the double-arrow to reveal it.
Create a new snippet
Click New snippet and give it a name like test.js.
Write your code
Type your code into the editor panel.
Run it
Press Ctrl+Enter on Windows/Linux or Cmd+Enter on Mac.
Snippets persist across browser restarts and tab navigation. They are perfect for keeping utility code, test scripts, and learning exercises that you run repeatedly.
Here is a useful snippet to try:
// Count elements by tag name on any page
const tags = {};
document.querySelectorAll("*").forEach(el => {
const tag = el.tagName.toLowerCase();
tags[tag] = (tags[tag] || 0) + 1;
});
console.table(tags);Save this as a Snippet and run it on different websites. It shows you which HTML elements each page uses most.
Running Snippets on Any Page
Snippets run in the context of whatever page you are viewing. Write a Snippet once, then open different websites and run it to see how they differ. This is a fast way to explore how real websites are built.
Debugging with Breakpoints
Breakpoints let you pause JavaScript execution at a specific line and inspect what is happening. This is how professional developers find bugs.
Setting a Breakpoint
Go to the Sources panel
Open DevTools and click Sources.
Find your file
Use the left file navigator to locate your JavaScript file under the page's domain.
Click a line number
A blue marker appears where you want execution to pause.
Trigger the code
Click a button, submit a form, or reload the page.
Execution pauses
The browser stops at the breakpoint you set.
While paused, you can:
- Hover over any variable to see its current value
- Look at the Scope panel on the right to see all local and global variables
- Look at the Call Stack panel to see the chain of function calls that led here
- Use the step controls (top-right of the Sources panel) to advance line by line
The debugger Statement
You can also set breakpoints directly in your code:
function calculateTotal(price, quantity) {
debugger; // Execution pauses here when DevTools is open
const total = price * quantity;
return total;
}The debugger statement acts like a breakpoint set in DevTools. When DevTools is open, execution pauses at that line. When DevTools is closed, it is ignored. This is useful for debugging code you wrote locally.
A Real Debugging Session
Here is a typical debugging workflow:
Notice the bug
A button click does nothing, and there are no errors in the Console.
Find the handler
Open the Sources panel and find the click handler function.
Set a breakpoint
Set it on the first line of that function.
Trigger it
Click the button. Execution pauses at the breakpoint.
Inspect the variables
Hovering over them shows the button selector returns null, meaning you used the wrong ID.
Fix and retest
Fix the ID, remove the breakpoint, and test again. It works.
This cycle of finding the problem, pausing, inspecting, fixing, and testing is the core of JavaScript debugging. The faster you get comfortable with breakpoints, the faster you will solve problems.
For more on how the browser processes the JavaScript you write in DevTools, see how browsers read and execute JavaScript code. If you want to learn both browser and server-side JavaScript execution, read about how to run JavaScript in the browser and Node.js.
Rune AI
Key Insights
- Open DevTools with Ctrl+Shift+I (Windows) or Cmd+Option+I (Mac).
- The Console runs JavaScript instantly and shows output and errors.
- Snippets in the Sources panel save code that persists across browser restarts.
- Breakpoints pause code execution so you can inspect variables and the call stack.
- Use console.log(), console.table(), and console.error() for different output types.
Frequently Asked Questions
Can I use DevTools in browsers other than Chrome?
Does code I write in the Console save automatically?
Can I edit a running website's JavaScript with DevTools?
Conclusion
Chrome DevTools is the most powerful tool a JavaScript developer has. The Console lets you run code instantly. The Sources panel lets you save Snippets that persist across sessions. Breakpoints let you pause code mid-execution and inspect what is happening. Learning these three panels is one of the highest-return investments you can make as a beginning JavaScript developer.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.