Run JavaScript in Chrome & Firefox: Step-by-Step
In this article
Running JavaScript directly in your browser is a quick way to test code, debug a page, or experiment with web APIs without setting up a separate development environment. Both Chrome and Firefox include a built-in console where you can type and execute JavaScript commands instantly. All you need is a modern version of either browser and a web page open (any page works, even a blank tab). This guide walks you through opening the console, running commands, and handling common issues.
What You'll Need
- A computer with Google Chrome or Mozilla Firefox installed (any recent version).
- A web page open in the browser (you can use a new tab with any site, or about:blank).
- Basic familiarity with typing and pressing Enter—no coding experience required.
Step-by-Step
- Open the browser developer tools. In Chrome, press F12 or Ctrl+Shift+I (Windows/Linux) or Cmd+Option+I (Mac). In Firefox, the same shortcuts work. Alternatively, right-click anywhere on the page and select Inspect (Chrome) or Inspect Element (Firefox).
- Switch to the Console tab. In the developer tools panel that opens, click the Console tab at the top. If you don't see it, click the >> or More tabs button to reveal it. The console shows a blinking cursor with a
>prompt (Chrome) or a>>prompt (Firefox). - Type your first JavaScript command. For example, type
console.log('Hello, world!');and press Enter. The console will outputHello, world!immediately. You can also type simple math like2 + 2and press Enter—it returns4. - Run multi-line commands. For longer code, use Shift+Enter to add a new line without executing. This lets you write functions or loops. For example:
function greet(name) { return 'Hello, ' + name; } greet('Alex');Press Enter after the last line to run it. - Access the current page's DOM. The console runs in the context of the open page. Try
document.titleto see the page's title, ordocument.body.style.backgroundColor = 'lightblue'to change the background color instantly. - Clear the console. Type
clear()and press Enter, or click the broom icon (Chrome) / trash icon (Firefox) in the console toolbar. - Close the developer tools. Press F12 or Ctrl+Shift+I again, or click the X on the panel.
Common Mistakes
- Forgetting semicolons: JavaScript often works without them, but for multi-line code, missing semicolons can cause unexpected behavior. Always add them at the end of statements.
- Using single quotes inside single quotes: For example,
console.log('It's a test');will throw a syntax error. Use double quotes or escape the inner quote:console.log("It's a test");. - Running code that changes the page permanently: Changes you make to the DOM are temporary and disappear when you refresh the page. Don't worry about breaking anything—just refresh to reset.
- Typing in the wrong panel: Make sure you're in the Console tab, not the Elements or Sources tab. The console prompt is where you type.
- Expecting results from
document.write(): This method can replace the entire page content if used after the page loads. Avoid it in the console; useconsole.log()instead.
Tips
- Use the console as a calculator: Type any arithmetic expression (e.g.,
123*456) and press Enter—it returns the result instantly. - Explore objects: Type
windowordocumentand press Enter to see a list of properties. You can expand them in the console output. - Use autocomplete: Start typing a command like
document.and a dropdown appears with available properties and methods. Use arrow keys and Tab to select. - Persist history: The console remembers your commands even after you close and reopen it, as long as you don't clear the browser data. Use the up arrow to cycle through past commands.
- Run code on page load: If you need to run code after the page loads, use
window.onload = function() { ... }in the console, but note that the page is already loaded by the time you open the console.
Troubleshooting
- Console is not showing: If pressing F12 doesn't work, try Ctrl+Shift+J (Chrome) or Ctrl+Shift+K (Firefox) to open the console directly. On Mac, use Cmd+Option+J (Chrome) or Cmd+Option+K (Firefox).
- Your command returns
undefined: This is normal. The console shows the return value of your command; if there's no explicit return, it showsundefined. For example,console.log('hi')printshiand then returnsundefined. - Syntax errors: If you see a red error message like
Uncaught SyntaxError, check for missing parentheses, brackets, or quotes. The error message usually points to the line number—click it to see the code. - Code works in one browser but not the other: Some JavaScript features are browser-specific. For example,
console.table()works in both, but older versions may not support it. Use widely supported methods likeconsole.log(). - Changes don't persist: As mentioned, DOM changes are lost on refresh. If you need to test code repeatedly, consider using the browser's Sources panel to create a snippet (Chrome) or use an online sandbox.
- Console is blocked by a website: Some sites disable the console via JavaScript. You can usually still open it, but commands may not work. Try using a blank tab (about:blank) to test code.
Frequently Asked Questions
Can I run JavaScript without opening the developer tools?
You can type JavaScript directly into the browser's address bar, but most modern browsers block this for security reasons. The console is the intended way.
Will running JavaScript in the console harm my computer?
No, the console runs in a sandboxed environment. It can only affect the current page and your browser session, not your operating system.
Can I save my console commands for later use?
Chrome allows you to save snippets in the Sources panel. In Firefox, you can copy commands to a text file. The console history itself is saved temporarily.
Why does my code work in the console but not in a separate HTML file?
The console runs in the context of the current page, so it has access to the page's DOM. In a separate HTML file, you need to include the script after the DOM elements, or use DOMContentLoaded event.
Can I run JavaScript on any website?
Yes, you can run commands on any page, but be aware that some sites use Content Security Policy (CSP) that may block certain commands. This is for security, and you shouldn't try to bypass it.
Is there a difference between Chrome and Firefox console commands?
Most commands are identical, but some browser-specific APIs exist. For example, chrome object is only in Chrome, and moz is only in Firefox. Stick to standard JavaScript for cross-browser compatibility.