Created
June 15, 2024 10:49
-
-
Save sahilatahar/e2e1d90fef8d2c41e8f0e6b8ec37d62a to your computer and use it in GitHub Desktop.
This JavaScript code demonstrates essential console APIs (log, warn, error, info, table) for debugging JavaScript applications in the browser. It includes examples for logging messages, handling warnings and errors, formatting data, and measuring execution time. Each method illustrates practical use cases to enhance debugging efficiency and appl…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Console APIs provide developers with powerful tools to debug JavaScript code, output information, and interact with the browser environment. Here are some practical use cases of console APIs along with examples: | |
// 1. Logging Messages (console.log()) | |
// Description: Output debug information, variables, or state of an application. | |
// Special Use Cases: Logging variables, objects, or messages for debugging purposes. | |
// Example: | |
let name = 'John'; | |
let age = 30; | |
console.log(`User ${name} is ${age} years old.`); | |
// 2. Warning Messages (console.warn()) | |
// Description: Highlight potential issues or warnings in the code. | |
// Special Use Cases: Alerting about high temperatures, deprecated APIs, or potential performance bottlenecks. | |
// Example: | |
let temperature = 110; | |
if (temperature > 100) { | |
console.warn('High temperature alert!'); | |
} | |
// 3. Error Messages (console.error()) | |
// Description: Log errors and exceptions for debugging purposes. | |
// Special Use Cases: Handling critical errors, invalid inputs, or unexpected behaviors. | |
// Example: | |
function divide(a, b) { | |
if (b === 0) { | |
console.error('Division by zero!'); | |
return NaN; | |
} | |
return a / b; | |
} | |
divide(5,0) | |
// 4. Informational Messages (console.info()) | |
// Description: Provide informative messages or status updates. | |
// Special Use Cases: Notifying application startup, state changes, or important events. | |
// Example: | |
console.info('Application started...'); | |
// 5. Table Formatting (console.table()) | |
// Description: Display tabular data for easier inspection and debugging. | |
// Special Use Cases: Visualizing arrays or objects in a structured format. | |
// Example: | |
const users = [ | |
{ id: 1, name: 'Alice', age: 28 }, | |
{ id: 2, name: 'Bob', age: 32 }, | |
{ id: 3, name: 'Carol', age: 25 } | |
]; | |
console.table(users); | |
// 6. Grouping Messages (console.group() and console.groupEnd()) | |
// Description: Organize related log messages into collapsible groups for better readability. | |
// Special Use Cases: Grouping logs for complex operations or debugging sessions. | |
// Example: | |
console.group('Fetching Data'); | |
console.log('Requesting data...'); | |
// Code to fetch data | |
console.log('Data received.'); | |
console.groupEnd(); | |
// 7. Counting Occurrences (console.count()) | |
// Description: Count how many times a particular piece of code is executed. | |
// Special Use Cases: Monitoring loop iterations, function calls, or event triggers. | |
// Example: | |
function processTask(task) { | |
console.count('Task Processed'); | |
// Processing logic | |
} | |
// 8. Timing Execution (console.time() and console.timeEnd()) | |
// Description: Measure the execution time of a code block or function. | |
// Special Use Cases: Performance profiling, optimizing critical sections, or benchmarking. | |
// Example: | |
console.time('Fetch Data'); | |
// Code to fetch data | |
console.timeEnd('Fetch Data'); | |
// 9. Assertions (console.assert()) | |
// Description: Assert conditions and log an error message if the condition is false. | |
// Special Use Cases: Validating assumptions, checking state consistency, or debugging conditions. | |
// Example: | |
let isAuthenticated = false; | |
console.assert(isAuthenticated, 'User is not authenticated!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment