JavaScript Console Trace() Method

JavaScript Console Trace() Method with Examples

In this article, I am going to discuss JavaScript Console Trace() Method with Examples. Please read our previous article where we discussed JavaScript Console Time() Method with Examples.

JavaScript console.trace() Method

Most of the time we faced issues debugging a function or piece of code. Understanding how the execution flows, for that console.trace() method is used.

The console.trace() method is used to print the stack trace that shows how the code is executed or the code flow in the web console. This method logs the call stack that exists at the point of what console.trace() method is invoked. It is used to find an error in the function that is being called from different places in the code. It is work by placing the console.trace() method in the code at the place where we want to trace the code from. It accepts an argument that is displayed as a label for the particular stack trace.

Syntax: console.trace(…data);

Parameters:
  1. data – This is an optional parameter. data can be any object that needs to be printed along with the trace to the web console.

What we discussed above is given in the below example.

Example to Understand JavaScript Console Trace() Method
<html>
<head>
    <title>JavaScript console trace() method example</title>
</head>
<body>
    <script>
        //sample code1
        function func1() {
            function func2() {
                console.trace();
            }
            func2();
        }
        func1();

        //sample code2
        function traceTest(a, b) {
            console.trace(" Start Trace");
            return a + b;
        }
        traceTest(23, 3);

        //sample code3
        function addition() {
            addNumbers();
        }

        function addNumbers() {
            console.trace('addition trace');
        }
        addition();
    </script>
</body>
</html>

Output: Press F12 and go to the Console section

Example to Understand JavaScript Console Trace() Method

In the next article, I am going to discuss JavaScript Console Warn() Method with Examples. Here, in this article, I try to explain JavaScript Console Trace() Method with Examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *