Home > Web Front-end > JS Tutorial > body text

How to debug code when js encounters code problems

angryTom
Release: 2020-02-05 17:54:19
forward
2172 people have browsed it

This article introduces the debugging method if you encounter problems when writing JavaScript code. I hope it will be helpful to all students learning JavaScript!

How to debug code when js encounters code problems

How to debug code when js encounters code problems

Single-step tracking debugging debugger;

The console watch function checks the current value of the variable

Enters the function operation

As it continues Click to continue looping, and the value of the specified variable is also changing

Add a breakpoint

Jump in Jump out of the function

throw new Error() actively throws an exception

The following code will no longer run

The code will jump Go to the try statement closest to this sentence

Use

try{
}catch(e){
}
Copy after login

Receive exception


##

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        try{
            var foo={};
            console.log(foo.pro);
        }catch(e){
            console.log(e);//undefined
        }finally{
            console.log(&#39;异常导致程序中止啦~&#39;);//异常导致程序中止啦~
        }
    </script>
</body>
</html>
Copy after login


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script>
        function multi(num1, num2){
            if(typeof num1 != "number" || typeof num2 != "number"){
                throw new Error(&#39;必须输入数字!!!&#39;);
            }
            console.log(num1*num2);
        }

        try{
            //multi("a", "b");//Error: 必须输入数字!!!
            multi(1, 2);//2

        }catch(e){
            console.log(e);
        }finally{
            console.log(&#39;不管有没有异常我都要执行哈~&#39;);
        }
    </script>
</body>
</html>
Copy after login
This article comes from the

js tutorial column, welcome to learn!

The above is the detailed content of How to debug code when js encounters code problems. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template