JavaScript:
Виведення налагоджувальної інформації
How to:
“Як це зробити:” Print a simple message to the console:
console.log("Hello, debug world!");
Output:
Hello, debug world!
Show a variable’s value:
let someValue = 42;
console.log("The value is:", someValue);
Output:
The value is: 42
Catch and display an error:
try {
throw new Error("Oops, an error occurred!");
} catch (error) {
console.error("Caught an error:", error);
}
Output:
Caught an error: Error: Oops, an error occurred!
Deep Dive
“Поглиблений Занурення”
Historical context: console.log
has been a part of JavaScript for ages. Formerly, programmers used alert dialogs (alert()
) or document writing (document.write()
) for debug output, which were intrusive and limited.
Alternatives: Apart from console.log
, there are console.info
, console.warn
, and console.error
for different levels of importance. Tools like debuggers, profilers, and IDEs often provide more advanced debug capabilities.
Implementation details: The console
object’s methods work with web browsers and Node.js. They can handle multiple arguments, format them, and even display complexity like objects and stack traces.
See Also
“Дивіться Також”
- MDN Web Docs on
console
: https://developer.mozilla.org/en-US/docs/Web/API/console - Node.js documentation on
console
: https://nodejs.org/api/console.html - An overview of JavaScript debugging with Chrome DevTools: https://developers.google.com/web/tools/chrome-devtools/javascript