JavaScript(JS)基础(二)

7、运算符

html 复制代码
<script>
    // 赋值运算符
    let num1 = 1
    num1 += 1
    console.log(num1);

    // 一元运算符
    let num2 = 2
    console.log(num2++ + --num2 - ++num2 + --num2)

    // 比较运算符
    let a = 1
    let b = "1"
    // 相等,值相等即可,不需要考虑数据类型
    console.log(a == b)
    // 全等,值和数据类型必须全部相等
    console.log(a === b)

    // 逻辑运算符
    console.log(true && false)
    console.log(true || false)
    console.log(!5)
</script>

8、分支语句

8.1 单分支语句
html 复制代码
<script>
    // 单分支语句,条件为真则执行语句
    if ("") {
        console.log("语句1");
    }
    if (" ") {
        console.log("语句2");
    }
</script>
8.2 双分支语句
html 复制代码
<script>
    let year = +prompt("输入年份")
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
        alert(`${year}为润年`)
    }
    else {
        alert(`${year}为平年`)
    }
</script>

9、switch语句

  • 计算器加减乘除的实现
html 复制代码
<script>
    num1 = +prompt("请输入第一个数字:")
    num2 = +prompt("请输入第二个数字:")
    a = prompt("请输入计算方式:")
    switch (a) {
        case "+":
            alert(num1 + num2)
            break
        case "-":
            alert(num1 - num2)
            break
        case "*":
            alert(num1 * num2)
            break
        case "/":
            if (num2 != 0)
                alert(num1 / num2)
            else
                alert("被除数不能为0")
            break

        default:
            break
    }
</script>

10、循环结构

  • 打印1到100除去8的偶数案例实现
html 复制代码
<script>
    let num = 1
    while (num <= 100) {
        if (num == 8) {
            num++
            continue
        }
        if (num % 2 == 0) {
            document.write(num + "--")
        }
        num++
    }
</script>
相关推荐
Zhou1411365 分钟前
SpringSecurity_02_授权与高级功能
开发语言·python
码匠许师傅39 分钟前
【C++三方组件】cpp-httplib:一个头文件起 HTTP 服务
开发语言·c++·http
莪_幻尘1 小时前
Skill 体检:30 个 Skill 全凭感觉?体检器先自曝了 8 个“假 0 分
前端·人工智能·llm
风骏时光牛马1 小时前
AI模型综合能力评测:性能、指令遵循与多场景实测对比
前端
Frag0ut1 小时前
Chrome与Chromium内核浏览器在Windows 11上的新特性全景解析
前端·chrome·windows·web安全·chromium·gemini ai·playready drm
小小小小钰儿2 小时前
1.5-Python网络编程
开发语言·网络·python·web安全·计算机·网络安全
hiahiahia1232 小时前
实现完整 Tool Dispatcher
开发语言·前端
IT_陈寒2 小时前
Java线程池这破玩意,差点让我周末加班排查到凌晨
前端·人工智能·后端