28 類型判斷 typeof
//查看變量的類型**
let test = “b”
console.log(typeof b);
變量 | 類型 |
---|---|
字符串 | string |
數組 | Object |
對象 | Object |
函數 | function |
沒有定義的變量 | undefined |
29 判斷變量是否聲明
if(typeof test == “undefined”){
throw new Error(“變量test沒有聲明”);
}
30 區分 數組和對象 instanceof
//查看變量是否爲對應的類型創建的
let shuzu = [];
let duixiang = {};
//如果是數組就返回true
console.log(shuzu instanceof Array);
//如果是對象就返回true
console.log(duixiang instanceof Object);
31 字符串轉義
let str = new String("zifuchuan");
console.log(str.toString());
簡化 同↓
let str1 = "zifuchuan1";
console.log(str1);
轉移符使用
轉義詞 | 轉義方法 |
---|---|
tab | \t |
回車 | \n |
" | \" |
\ | \\ |
空格 |  ; |
其他簡單轉義方式
tab鍵上邊的按鍵 · 進行正常輸入模式的字符串定義
原來的字符串定義:
let str = “ab”+“c\d”;
ES6新字符串定義:
let str1 = ·abc\d·;