NodeJS Console
NodeJS Console
The NodeJS console module provides a simple debugging console similar to the JavaScript console available in web browsers. These methods write to standard output or standard error and are useful for logging, debugging, and inspecting data.
| Console Method | Description |
|---|---|
| log() | Prints standard output (stdout) with a newline. |
| error() | Prints standard error (stderr) with a newline. |
| warn() | Alias for error(). |
| info() | Alias for log(). |
| dir(obj) | Uses util.inspect() and prints a readable representation of an object. |
| time(label) | Starts a timer that can be used to measure the duration of an operation. |
| timeEnd(label) | Stops a timer started by time() and prints the elapsed time. |
| trace() | Prints a stack trace showing the current call path. |
| assert(value, message) | Throws an AssertionError when the condition is false. |
| table(data) | Displays tabular data in a readable table format. |
Console Example
console.log('Hello Tutorials Logic!');
console.error('Error');
console.warn('Warning');
console.info('Information');
console.time('timer');
for (let i = 0; i < 100000; i++) {}
console.timeEnd('timer');
Key Takeaways
- Node.js provides a built-in console module for logging, debugging, and inspecting values.
-
console.log()writes to standard output, whileconsole.error()writes to standard error. -
console.warn()andconsole.info()are convenience aliases for warning and informational logs. -
Use
console.time()andconsole.timeEnd()to measure execution time. -
console.dir()andconsole.table()help inspect objects and arrays in a readable format. - Console methods are helpful during development, but sensitive information should never be logged in production.
Related Node.js Topics