JavaScript(二)(未整理完)
javascript操作符
算術運算子
- 算術運算子有:+,-,*,/,%,++,--
對於算術運算子要注意的是:字串和數字之間的相加減,以及布林值型別和字元,數字之間的相加減
var numa = 10;
var numb = 4;
document.write(numa+numb); //14
console.log(numa-numb); //6
console.log(numa*numb); //40
console.log(numa/numb); //2.5
var str = “1”;
console.log(numa+str); //101
console.log(numa-str); //9
console.log(numa+true); //11
console.log(numa-false); //10
console.log(numa+null); //10
console.log(numa+undefined); //NaN
console.log(0.1+0.2);
賦值運算子
- 賦值運算子有:=,+=,-=,*=,/=
var num = 10;
console.log("=",num); //= 10
num += 5; //num = num + 5
console.log("+=",num); //+= 15
num -= 4; //num = num - 4
console.log("-=",num); //-= 11
num /= 3; //num = num / 3
console.log("/=",num); // /= 3.6666666666666665
num = 2; //num = num * 2
console.log("=",num); //*= 7.333333333333333
num %= 1; //num = num % 1
console.log("%=",num); //%= 0.33333333333333304
比較運算子
- 比較運算子有:>,>=,<,<=,==,===(全等)
- 比較運算子只會返回布林值:true/false
var a = 10;
var b = 15;
var c = ‘10’;
console.log(a>b); //false
console.log(a>=b); //false
console.log(a<b); //true
console.log(a<=b); //true
console.log(ac); //true 比較數值
console.log(a=c); //false
邏輯運算子
- 邏輯運算子有:&&(與),||(或),!(非) - 以下在進行判斷的時候為假 1.0 2.null 3.undefined 4.NaN 5.' '(空字串) 6.false 需要注意:邏輯運算子兩邊的數值均為布林值,或者其他資料型別
var a = 10>4 && 3>4;
console.log(a); //false
var b = 10>5 && 3>2;
console.log(b); //true
var c = 1>2 || 2<5;
console.log©; //true
var d = 1 || 0 || ‘’;
console.log(d); //1
var e = 22 && undefined || null;
console.log(e); //null
var f = !0;
console.log(f); //true
var g = 10 || 3 && false;
console.log(g); //10
var h = 1 && null && undefined;
console.log(h); //null
javascript流程控制
- if(判斷條件){
顯示結果
}
javascript迴圈
for迴圈
for(迴圈條件){
顯示結果
}
for (var i=1;i<10;i++) { #注意不要忘了i++,如果沒有i++就是死迴圈,無法執行
document.write(i)
} #123456789
九九乘法表:
<script type="text/javascript">
for (var i=1;i<=9;i++) {
document.write('<br>')
for (var j=1;j<=i;j++){
var sum = i*j;
document.write(i+"*"+j+"="+sum+" "); +是連線作用
}
}
while迴圈
while(迴圈條件){
顯示結果
}
先判斷再執行
var a = 0;
while (a<10){
document.write(a);
a++;
}
do.while迴圈
do{
顯示結果
}while{
迴圈條件
}
先執行再判斷
var b = 1;
do {
document.write(b);
b++;
}while (b<10)
javascript字串方法
length:長度
[ ]/charAt():下標
indexOf():索引
Replace():替換
Split():分割
toUpperCase():大寫
toLowerCase():小寫
Substring():擷取
Slice():切割
var str1 = 'hello world';
document.write(str1.length+"
"); // 11
document.write(str1[0]+"
") ; //h
document.write(str1.indexOf(“l”)+"
");
document.write(str1.split(" “)+”
"); //把分割點變成逗號
document.write(str1.slice(0,4)+"
"); //hell,左閉右開
document.write(str1.slice(4,2)+"
"); //空,左閉右開,考慮順序
document.write(str1.substring(1,4)+"
"); //ell
document.write(str1.substring(4,2)+"
"); // ll,不考慮順序,依舊左閉右開
document.write(str1.replace(‘he’,“h”)+"
"); //hllo world
document.write(str1.replace(‘o’,“m”)+"
"); //hellm world,替換第一個
javascript陣列方法
陣列常用方法:
length:長度
[]:下標
push():追加
Unshift():新增
Pop():後刪
Shift();前刪
indexOf():索引
slice();切片
splice():替換
join():拼接
Sort():排序
Reverse():反向
Concat():連線
補充方法:
toString():字串
toFixed():小數字符串
isNaN():判斷NaN
isArray():判斷陣列
parseInt():整數
parseFlost():浮點數
Number():數字
練習