console.log
console.log prints its arguments to the terminal, separated by a space, and ends with a newline.
console.log is a function that comes with Node. It takes any number of values and writes them to standard output, side by side, separated by a space. It adds a newline at the end.
Strings, numbers and booleans look the way you expect. An object or an array is printed too, but in Node's inspection format, not as JSON. That's why in this course, when you want to see exactly what an object holds, you use JSON.stringify.
In lesson 00.02 you see it like this: console.log(city.toUpperCase(), year, isLearning);. Each argument is evaluated first, then everything is printed on one line.
It's the quickest way to check what something holds while you learn. TypeScript only checks that the call is valid; when Node runs the code, types are gone.
Reach for it when: you want to see a value or show a program's result in the terminal.
Examples
Common mistake
const user = { name: "Ana" };
console.log("user: " + user);Why: With `+`, the object is converted to a string and loses its contents.
How to write it: Pass it as a separate argument or use JSON.stringify: console.log("user:", JSON.stringify(user)).
