Introduction
For debugging of our JavaScript code, we use console.log object a lot. This helps us to determine and troubleshoot the issue on a web page in the browser. This console object can be used during the development process and also enables the developer to see the value of an expression in the specific page context. In general, it facilitates monitoring, writing, and evaluating in the browsers' developer tool (F12).
There are a few more important members of this Object and that can make our debugging a lot easier.
Tracing of the Operation/Step
Grouping of logs
Display of logs
The execution time of operation
There are a few more methods in console object but most of them are not standard and not supported by all the browsers.
This article was originally published on
taagung and the complete code used in this article is available here for your practice.
- <!DOCTYPE html>
- <html>
- <body>
- <h1>JavaScript console Introduction</h1>
- <script>
- console.log("My New Log");
- console.info("this is info log");
- console.error("Error log");
- console.warn("Warning log");
- console.group("Parent Group");
- console.groupCollapsed("My New Group");
- console.log("Begininig of the group");
- console.log("Log Number #1");
- console.log("Log Number #2");
- console.log("Log Number #3");
- console.groupEnd();
-
- console.log("Out of the Group now");
- console.groupEnd();
-
- console.clear();
- console.table(["taagung", "C# Corner", "MSDN"]);
-
- function Article(authorName, articleName) {
- this.authorName = authorName;
- this.articleName = articleName;
- }
- var author = {};
-
- author.atul = new Article("Atul Sharma", "SOUND Programming Methodology");
- author.tuba = new Article("Tuba Mansoor", "Angular Tutorial");
- author.mahesh = new Article("Mahesh Chand", "C# Corner Plateform");
-
- console.table(author);
-
- myFirstMethod();
-
-
- function myFirstMethod() {
- mySecondMethod();
- }
-
- function mySecondMethod() {
- myThirdMethod();
-
- }
-
- function myThirdMethod() {
- console.trace();
- }
-
- TimeElapsed();
- function TimeElapsed() {
- console.time("PositiveForloopTime");
- for (var i = 0; i < 100000; i++) {
-
- }
- console.timeEnd("PositiveForloopTime");
-
- console.time("NegativeForloopTime");
- for (var i = 100000; i < 0; i--) {
-
- }
- console.timeEnd("NegativeForloopTime");
- }
-
- CalledFunction();
- CalledFunction();
- CalledFunction();
-
- function CalledFunction() {
- console.count("Called Count");
- }
- </script>
-
- </body>
- </html>