JavaScript: Enhance Console Logging with Timestamps
This code snippet demonstrates how to enhance JavaScript's console.log functionality by adding a timestamp to each log message. This improves clarity and makes debugging simpler.
function log(message) {
console.log(`[${new Date().toLocaleString()}] ${message}`);
}
log('Hello, World!');
The log function takes a message as input and outputs it to the console along with the current time formatted as a localized string. Now, every log entry will display the exact time it was generated, making it easier to track events and understand the flow of your code.
AI 优化版
原文地址:https://www.cveoy.top/t/topic/mTxS 著作权归作者所有。请勿转载和采集!