Beginner Fundamentals
The console Object
The console object is your main tool for printing output and debugging. It has more methods than just log.
log, error, and warn
console.log("Normal message");
console.error("Something went wrong"); // goes to stderr
console.warn("Be careful"); // a warning
console.error and console.warn write to the error stream, which is useful for separating errors from normal output.
Printing variables
You can pass several values, and Node prints them separated by spaces:
const user = { name: "Fausto", age: 30 };
console.log("User:", user); // User: { name: 'Fausto', age: 30 }
console.table
For arrays and objects, console.table shows a neat grid:
const fruits = [
{ name: "apple", price: 2 },
{ name: "banana", price: 1 },
];
console.table(fruits);
Timing code
console.time("loop");
for (let i = 0; i < 1000000; i++) {}
console.timeEnd("loop"); // loop: 3.2ms
Summary
Use console.log for normal output, error and warn for problems, and table to display data clearly while debugging.