一,基本运算符
-
算数运算符(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
-
比较运算符(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>");// 比较值大小,并且比较类型
-
逻辑运算符
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>"); // 非,这里的一个等于相当于上面的两个等于
二,控制流程
-
if语句
<script> var name = "爸爸"; if (name == "邢爸爸") { alert("我是你邢爸爸"); }else if (name == "爸爸") { alert("我是你龚孙子"); } </script>
-
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
-
while循环
<script> var a = 0; while (a < 10) { document.write(a); a++; } </script>
-
do while循环
<script> var b = 0; do { document.write(b); b++; }while (b < 8); </script>
四,字符串方法
-
长度(测量长度)
<script> var str1 = "hello world"; document.write(str1.length, "<p></p>"); </script>
-
通过索引找字符
<script> var str1 = "hello world"; document.write(str1[0], "<p></p>"); </script>
-
通过字符找到索引
<script> var str1 = "hello world"; document.write(str1.indexOf("h"), "<p></p>"); </script>
-
分隔
<script> var str1 = "hello world"; document.write(str1.split('l'), "<p></p>"); </script>
-
切割
<script> var str1 = "hello world"; document.write(str1.slice(0, 2), "<p></p>"); </script>
-
截取(可以反向截取,能比较索引大小)
<script> var str1 = "hello world"; document.write(str1.substring(0, 5), "<p></p>"); </script>
-
替换
<script> var str1 = "hello world"; document.write(str1.replace("hello", "我是"), "<p></p>"); </script>
五,数组(相当于python的列表)的方法
-
长度
<script> var li = ["兔子", "萝卜", "铁锅"]; document.write(li.length, "<p></p>"); // "<p></p>"是在打印的时候换行 document.write(li[0], "<p></p>"); // 打印索引为0的元素 </script>
-
追加(在数组最末尾加,会返回数组的长度)
<script> document.write(li.push("香菜"), "<p></p>"); document.write(li, "<p></p>"); </script>
-
添加(在数组最前面加,会返回数组的长度)
<script> document.write(li.unshift("火锅底料"), "<p></p>"); document.write(li, "<p></p>"); </script>
-
后删(返回删除的元素)
<script> document.write(li.pop(), "<p></p>"); </script>
-
前删
<script> document.write(li.shift(), "<p></p>"); </script>
-
根据元素返回索引值
<script> document.write(li.indexOf("萝卜"), "<p></p>"); </script>
-
切片
<script> document.write(li.slice(0, 3), '<p></p>'); </script>
-
替换(返回被替换的元素)
<script> document.write(li.splice(0, 2, "兔子头"), "<p></p>"); document.write(li, "<p></p>"); </script>
-
拼接(会把里面元素通过传入的参数拼接起来)
<script> document.write(li.join("~"), "<p></p>"); </script>
-
链接(两个数组连在一起)
<script> var c = [1, 2, 3, 4]; document.write(c.concat(li), '<p></p>'); </script>
-
排序(根据编码的顺序排序)
<script> var d = [1, 5, 95, "ww", "我手机", 100]; document.write(d.sort(), "<p></p>"); </script>
-
反向排序
<script> document.write(d.reverse()) </script>