Monday 29 October 2018

JavaScript: Monitor variables, expressions using watch

JavaScript debugger provides a watch section, where you can watch for the variables, expressions.

Let me explain with an example.

HelloWorld.html
<!DOCTYPE html>
<html>

<body>

    <script>
        function factorial(n) {
            if (n <= 0) {
                return 1;
            }
            var result = 1;

            for (var i = 1; i <= n; i++) {
                result *= i;
            }
            return result;
        }

        var fact = factorial(5);
        document.write("Factorial of 5 is : " + fact);
    </script>

</body>

</html>

Open ‘HelloWorld.html’ in browser.

Turn on debugger.

Add the debug point inside for loop.

Reload the page HelloWorld.html.
Click on + button in Watch section.

Type the variable result, to see the value of variable ‘result’.

Resume execution, you can observe value of result is changed to 2.


Previous                                                 Next                                                 Home

No comments:

Post a Comment