if语句和switch

1.if语句

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>if语句</title>
</head>

<body>
    <h2>if语句</h2>
    <p>语法:prompt()</p>
    <p>功能:弹出输入框</p>
    <p>返回值:</p>
    <p>1.点击确定,返回输入内容</p>
    <p>2.点击取消,返回null</p>

    <script>
        var password = prompt("请设置密码");
        //判断密码的长度,如果不是6位,否则
        if (password.length != 6) {
            alert("请输入6位的数字密码")
        } else {
            //如果密码是非数字,否则是数字
            if (isNaN(password)) {
                alert("密码必须是数字")
            } else {
                alert("密码设置正确")
            }
        }
    </script>

</body>

</html>

练习:

<script>
        var str = "abc123";
        var num = parseInt(str);
        // alert(num);//NaN 非数字开头
        if(num ==NaN){  //NaN和任何内容都不相等,包括它本身
            alert(NaN);
        }else if(num == 123){
            alert(123);
        }else if(typeof num == "number"){  //NaN类型属于number类型
            alert("num");
        }else{
            alert("str");
        }
        //结果弹窗  num
    </script>

猜你喜欢

转载自www.cnblogs.com/huanghuali/p/9613716.html