Console
Explore the browser console for running JavaScript, debugging code, and handling errors. Learn to open the console, execute commands, and view output.
Explore the browser console for running JavaScript, debugging code, and handling errors. Learn to open the console, execute commands, and view output.
The browser console is where JavaScript talks to you. It’s also where you can talk to JavaScript — run code, inspect values, and see what’s going wrong.
F12 → Console tab, or Ctrl+Shift+JF12 → Console tab, or Ctrl+Shift+KCmd+Option+CThe most-used tool for seeing what a value is at a given moment:
console.log("hello");console.log(42);console.log([1, 2, 3]);console.log({ name: "Reza", age: 28 });You can log multiple values at once:
const name = "Reza";const age = 28;console.log("name:", name, "age:", age);// name: Reza age: 28console.warn("this is a warning"); // yellow triangleconsole.error("something broke"); // red X — for errorsconsole.table([ { name: "a", val: 1 }, { name: "b", val: 2 },]);console.time("label");// ...code to measure...console.timeEnd("label"); // prints elapsed timeWhen something breaks, the console shows a red error message with a file name and line number. That’s where to look first:
TypeError: Cannot read properties of undefined (reading 'name') at getUserName (app.js:12) at main (app.js:5)Read bottom-up: the call started at main on line 5, reached getUserName on line 12, and that’s where it crashed.
You can type JavaScript directly into the console and run it with Enter. Useful for quick experiments without creating a file:
> [1, 2, 3].map(n => n * 2)< [2, 4, 6]The console is a REPL — Read, Eval, Print, Loop. It evaluates expressions immediately.