console.log and the console
The console is where JavaScript writes messages for developers. In a browser it lives in the developer tools (F12); on CodeMIRAI, console output appears right under your code.
console.log
console.log() prints any value. Give it several values and it prints them separated by spaces:
JavaScript
console.log("Hello!");
console.log(42, true, null);
console.log("Total:", 19.99 * 3);Logging objects and arrays
The console shows the structure of objects and arrays, which makes it ideal for inspecting data:
JavaScript
const user = { name: "Asha", age: 21, skills: ["JS", "Python"] };
console.log(user);
console.log([1, 2, 3].map(n => n * 10));Warnings, errors and tables
JavaScript
console.warn("This looks suspicious");
console.error("Something went wrong");
console.table([
{ name: "Asha", score: 92 },
{ name: "Leo", score: 78 },
]);console.table is wonderful for arrays of objects.
Reading an error
When code fails, the console shows the error's type and message, and usually the line:
JavaScript
const total = 100;
console.log(totl);ReferenceError: totl is not defined means you used a name that doesn't exist, here a typo of total. Learning to read errors calmly is the most useful debugging skill you can have.
Finished this lesson?Mark it complete to earn +20 XP and move your progress on.