JavaScript(2):基本运算符,控制流程,字符串和数组的方法

一,基本运算符

  1. 算数运算符(console.log使他在控制台显示)

    console.log(a + b); // 加
    console.log(a - b); // 减
    console.log(a * b); // 乘
    console.log(a / b); // 除
    console.log(a % b); // 取余
    console.log(++a); //自增长
    console.log(--a); //自减少
    console.log(number = 9); // 赋值运算符
    console.log(number += 9); // number = number + 9
    console.log(number -= 9); // number = number - 9
    console.log(number *= 9); // number = number * 9
    console.log(number /= 9); // number = number / 9
    console.log(number %= 9); // number = number % 9
    
  2. 比较运算符(document.write可以把内容写入网页页面)

    document.write(a > b); document.write("<p></p>");// 比较大小(后面是换行)
    document.write(a < b); document.write("<p></p>");  // 比较大小
    document.write(a >= b); document.write("<p></p>"); // 比较大小
    document.write(a <= b); document.write("<p></p>"); // 比较大小
    document.write(a == b); document.write("<p></p>"); // 比较值的到小
    document.write(a === b); document.write("<p></p>");// 比较值大小,并且比较类型
    
  3. 逻辑运算符

    document.write(a > b && a < c); document.write("<p></p>"); // 与
    document.write(a > b || a > c); document.write("<p></p>"); // 或
    document.write(a !== 10); document.write("<p></p>"); // 非,这里的一个等于相当于上面的两个等于
    
二,控制流程

  1. if语句

    <script>
        var name = "爸爸";
        if (name == "邢爸爸") {
        	alert("我是你邢爸爸");
        }else if (name == "爸爸") {
        	alert("我是你龚孙子");
        }
    </script>
    
  2. switch语句

    <script>
        var age = 1;
        switch (age) {
            case 1:
            document.write("<p>今天周一</p>");
            // break;
            case 2:
            document.write("<p>今天周二</p>");
            // break;
            case 3:
            document.write("<p>今天周三</p>");
            // break;
            case 4:
            document.write("<p>今天周四</p>");
            // break;
            case 5:
            document.write("<p>今天周五</p>");
            // break;
        }
    </script>
    

    ps:switch是提供了多个分支 但只是提供了程序入口 如果没有出口break 会在执行完一个case后执行下一个case直到switch结束或者遇到break

  3. while循环

    <script>
        var a = 0;
        while (a < 10) {
            document.write(a);
            a++;
        }
    </script>
    
  4. do while循环

    <script>
        var b = 0;
        do {
            document.write(b);
            b++;
        }while (b < 8);
    </script>
    
四,字符串方法

  1. 长度(测量长度)

    <script>
        var str1 = "hello world";
        document.write(str1.length, "<p></p>");
    </script>
    
  2. 通过索引找字符

    <script>
        var str1 = "hello world";
        document.write(str1[0], "<p></p>");
    </script>
    
  3. 通过字符找到索引

    <script>
        var str1 = "hello world";
        document.write(str1.indexOf("h"), "<p></p>");
    </script>
    
  4. 分隔

    <script>
        var str1 = "hello world";
        document.write(str1.split('l'), "<p></p>");
    </script>
    
  5. 切割

    <script>
        var str1 = "hello world";
        document.write(str1.slice(0, 2), "<p></p>");
    </script>
    
  6. 截取(可以反向截取,能比较索引大小)

    <script>
        var str1 = "hello world";
        document.write(str1.substring(0, 5), "<p></p>");
    </script>
    
  7. 替换

    <script>
        var str1 = "hello world";
        document.write(str1.replace("hello", "我是"), "<p></p>");
    </script>
    
五,数组(相当于python的列表)的方法

  1. 长度

    <script>
    	var li = ["兔子", "萝卜", "铁锅"];
        document.write(li.length, "<p></p>");	// "<p></p>"是在打印的时候换行
        document.write(li[0], "<p></p>");		// 打印索引为0的元素
    </script>
    
  2. 追加(在数组最末尾加,会返回数组的长度)

    <script>
        document.write(li.push("香菜"), "<p></p>");
        document.write(li, "<p></p>");
    </script>
    
  3. 添加(在数组最前面加,会返回数组的长度)

    <script>
    	document.write(li.unshift("火锅底料"), "<p></p>");
    	document.write(li, "<p></p>");
    </script>
    
  4. 后删(返回删除的元素)

    <script>
    	document.write(li.pop(), "<p></p>");
    </script>
    
  5. 前删

    <script>
    	document.write(li.shift(), "<p></p>");
    </script>
    
  6. 根据元素返回索引值

    <script>
    	document.write(li.indexOf("萝卜"), "<p></p>");
    </script>
    
  7. 切片

    <script>
    	document.write(li.slice(0, 3), '<p></p>');
    </script>
    
  8. 替换(返回被替换的元素)

    <script>
    	document.write(li.splice(0, 2, "兔子头"), "<p></p>");
    	document.write(li, "<p></p>");
    </script>
    
  9. 拼接(会把里面元素通过传入的参数拼接起来)

    <script>
    	document.write(li.join("~"), "<p></p>");
    </script>
    
  10. 链接(两个数组连在一起)

    <script>
        var c = [1, 2, 3, 4];
    	document.write(c.concat(li), '<p></p>');
    </script>
    
  11. 排序(根据编码的顺序排序)

    <script>
        var d = [1, 5, 95, "ww", "我手机", 100];
        document.write(d.sort(), "<p></p>");
    </script>
    
  12. 反向排序

    <script>
    	document.write(d.reverse())
    </script>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值