The provided web content offers a comprehensive guide to utilizing the JavaScript Console API for debugging and logging, emphasizing its importance for developers and showcasing various methods with examples and browser compatibility notes.
Abstract
The article delves into the JavaScript Console API, a crucial tool for developers, providing detailed explanations and examples of its methods such as assert, clear, count, debug, dir, dirxml, error, group, groupCollapsed, groupEnd, info, log, markTimeline, profile, profileEnd, table, time, timeEnd, timeLog, timeStamp, trace, warn, and memory. It highlights the API's role in debugging, performance analysis, and the visualization of data in a structured format. The author also discusses browser-specific features, compatibility issues, and recommends best practices for using the Console API effectively. The article aims to enhance developers' understanding of the Console API, encouraging them to use it for better code analysis and debugging.
Opinions
The author expresses a preference for using console.log(), console.warn(), console.error(), and console.info() for debugging due to their wide browser support and ease of use.
The article suggests that some Console API methods, such as console.dirxml() and console.profile(), are non-standard and should be used with caution, particularly in production environments.
The author recommends against using online JavaScript code editors for testing Console API methods, advising the use of a local browser and editor setup for clearer output.
There is an emphasis on the importance of checking browser compatibility, especially for methods like console.markTimeline() which may not be supported across all browsers.
The author values reader feedback and invites comments and suggestions, indicating a commitment to continuous learning and improvement in their educational content.
The conclusion of the article reflects the author's view that while the Console API is powerful, developers should be selective in which methods they use, considering browser support and the specific needs of their debugging tasks.
Javascript Console API?
Easy ways to Learn Basic to Advance All Console Web API with Demo
In This Article, We Learn Javascript Web Console API. I hope you enjoy š that and give me feedback š¢ or suggestion šš¼ about my work ā¤ļø.
Javascript Console API.
Console.< web API Name here >
Exmple:
console.log();
console.clear();
Demo:
Web Console API
assert
clear
count
countReset
debug
dir
dirxml
error
group
groupCollapsed
groupEnd
info
log
markTimeline
profile
profileEnd
table
time
timeEnd
timeLog
timeStamp
trace
warm
memory
Opening the console
In most current browsers, the javascript console is integrated with dev tools.
The shortcut keys help us open the console more easily.
I am opening the dev-tool panel in the Firefox console.
We are opening the dev-tool panel in the Firefox console.
1. Window/Linux
Ctrl + Shift + KCtrl + Shift + I// then click to console tab
2. Mac Os
Cmd + Opt + K
3. Edge and Internet Explorer
Opening the console panel in the developer tool F12, Click on the console tab.
4. Opera
We are opening the dev-tool panel in the Opera console.
Ctrl + Shift + I// Then click on the console tab
Console
Console part of the window global object. So check all associate property in the console, so write window. console. A window is a global object in the web browser.
window.console
Check the result in the web console
Compatibility:
When checking compatibility, provide information for developers and other staff. So in the case of using the if statement.
if(typeof window.console !== 'undifined'){
console.log('this working'); // some code here
}
if(window.console){
console.log('this is working') // some code here
}
Formatting console output
Many console methods can also handle string formatting, using % token.
Javascript provides a list of formatting specifiers in js.
Specifier:
%s: formatting the value as a string.
%i or %d: formatting the value as an integer.
%f: formatting the value as a floating-point value.
%o: formatting the value as an expandable DOM element.
%O: formatting the value as an expandable javascript object.
%c: Applies CSS style rules to the output string as specified by the second parameter.
console.log("Hello, My name is %s. You've called log %d. Floting value %f and javscript object %O", "Rajdeep Singh", 1, 6.7 ,{test:'testing', num:34});
console.warn("Hello, My name is %s. You've called warn %d. Floting value %f and javscript object %O", "Rajdeep Singh", 1, 5.7, {test:'testing', num:34});
console.info("Hello, My name is %s. You've called info %d. Floting value %f and javscript object %O", "Rajdeep Singh", 1, 6.75, {test:'testing', num:34});
console.error("Hello, My name is %s. You've called error %d. Floting value %f and javscript object %O", "Rajdeep Singh", 1, 6.7455, {test:'testing', num:34});
formatting Example %s, %f, %O
formatting Example %s, %f, %O
Formatting Example With CSS style
console.log("%cHello, My name is %s do working .", "color:red", "Rajdeep Singh" );
console.warn("%cHello, My name is %s . working", " color:green", "Rajdeep Singh");
Formatting with CSS
Formatting Example with %o
console.log("Hello, My name is %s. You've called log. %o", "Rajdeep Singh", document.body);
console.warn("Hello, My name is %s. You've called log. %o", "Rajdeep Singh", document.body);
console.error("Hello, My name is %s. You've called log. %o", "Rajdeep Singh", document.body);
console.info("Hello, My name is %s. You've called log. %o", "Rajdeep Singh", document.body);
formatting Example with %o
Note
The sequence is a matter of formatting. Right sequence Show Right Output.
// console.warn(" %c Hello, My name is %s do notwork .", "Rajdeep Singh", "color:green" ); // No Working Seuence matter
Assert
console.assert() method writes a message to the console, but only if an expression evaluates to false. Otherwise, if the expression evaluates to true, this does nothing.
Note
Like if a statement when the expression was true works; otherwise, no work. But this method work when returning false.
console.assert(expression , message)
expression = Boolen Value (true or false)
message = your custom message
You can clear the console window using the console.clear() method. this removes previously printed messages in the console tab. When using the clear method in your code, you show a message āconsole was clearedā in your console tab.
console.clear()
console.clear()
The web browser provides a console.clear()functionality in build in chrome browser you click on the console.
Click Settings Icon > click on preserve log
preserve to log in to the Chrome dev tool
Count
Each time when console.count() method invoked, the counter is increased by one. in your console tab, you see counter + label.
console.countReset() method helps to reset your counter. Based on the label, make sure your label is the same.
console.countReset( label here );
Exmple:
console.countReset(); // defaultconsole.countReset("test"); // reset counter base on labelconsole.countReset('test2'); // label not be same than not work
log
The console.log() method is very important for us. Because most time, we use the log method to learn another personās code.
I always use the log method daily.
console.log()
Exmple
console.log('rajdeep');
console.log(" my name is %s %s Exmple by formating log ", "rajdeep", "singh"); // formating console
look like your console with colour
Note:
The text Color is not the same in your browser.
debug
console.debug() method is another way to debug your code, like a console.log() method. The debug method provides you with a different colour in the console tab. console.debug()provides Blue Color text in the console tab.
console.dirxml() method use for debugging. this method prints value in the console tab form of an XML representation. Simple means that convert obj, array, etc. into XML form
console.group() and groupEnd() both create a group message in console tab. the label is optional In this method.
console.group('start group'); // label optionalconsole.log('start test 1');
console.warn('start test 2');
console.error('start test 3');
console.groupEnd('start group')
console.group() and groupEnd()
console.groupCollapsed() method create a close group with a small icon
console.groupCollapsed('start group'); // label optionalconsole.log('start test 1');
console.warn('start test 2');
console.error('start test 3');
console.groupEnd()
console.groupCollapsed() and console.groupEnd()
markTimeline:
console.markTimeline() method is not shown on Chrome and firefox. only working in Safari.
console.memory is not a function or method. Because when you use a console.memory() show TypeError Message in Console.
console.memory help shows a web browserās current memory information.
console.memory provides three properties.
jsHeapSizeLimit
totalJSHeapSize
usedJSHeapSize:
console.memory // right syntaxconsole.memory() // show type Error
Exmple:
console.memory.jsHeapSizeLimit
console.memoryconsole.memory TypeError
Code For Play
Note
I recommended using my web browser and editor for a test or play code. Please, make sure never to use an online javascript code editor. Because code run, but output maybe not show a clear.
My Conclusion on console web API is very different. Because of javascriptās peculiarlanguage. I recommended to you only log(), warn(),error(),and info() use for debugging.
Other web Api I never recommended you to because of some browser support.
In another region, some APIs work with Web Workers, and others show errors in the node.js.
If there are any queries, mistakes, and Suggestions. Please, tell me in the comment box.