一、字符串拓展
1.模板字符串
// ``
console.log(` \` `);
在模板字符串中 如果需要寫一個返點字符 罩旋,則要在 `前面加上。
2.字符串擴展的操作方法
1.includes()
let str = 'hellow';
console.log(str.includes('o'));//true
console.log(str.includes('a'));//false
// 查找指定字符 有返回值
// 能找到 返回true
// 找不到 返回false
2.starsWidth()
console.log(str.startsWith('h'));//true
console.log(str.startsWith('he'));//true
console.log(str.startsWith('hel'));//ture
console.log(str.startsWith('helo'));//false
console.log(str.startsWith('o'));//false
console.log(str.startsWith('w'));//false
// 判斷是否以 指定字符開頭
// 是 返回 true
// 不是 返回 false
3.endWith();
// 判斷是否以 指定字符結尾
// 是 返回 true
// 不是 返回 false
4.repeat()
console.log(str.repeat(1));
console.log(str.repeat(2));
console.log(str.repeat(3));
// 將原字符串 重復復制 指定次數 并將生成的新字符串 返回
5.trim()
let str1 = ' a b c d e f ';
console.log(str1.trim());// 刪除字符串前后空格
6.trimStart();刪除首位空格
7.trimEnd();刪除末尾空格
2.數值的拓展
Number新增方法
- Number.isNaN(變量); 判斷數值是否是 NaN
let num = 123;
let num1 = NaN;
let str = '123';
console.log(Number.isNaN(num));//false
console.log(Number.isNaN(num1));//true
console.log(Number.isNaN(str));//false
// 只跟 值是不是 NaN 有關系 與數據類型無關
2.Number.parseInt(變量);
let num = '1234.5';// 舍去小數位
console.log(Number.parseInt(num));
3.Number.parseFloat();轉成標準的小數 將多余的0去掉
let num1 = 1234.150000;
console.log(Number.parseFloat(num1));
4.isInteger(); 判斷是不是整數
let num2 = 123;//true
let num3 = 123.12;//false
console.log(Number.isInteger(num2));
console.log(Number.isInteger(num3));
計算平方
Math.pow(num,次方);
開平方
Math.sqrt(num);
開立方
Math.cbrt(num);
判斷一個數是不是正數
Math.sign()
正數返回1 負數返回-1 0返回0
新增運算符 ** 指數運算 相當于 Math.pow()
console.log(2 ** 4);//16
3.數組的拓展
- ...
有序集合
let arr = [1, 2, 3, 4, 5];
console.log(...arr);//1 2 3 4 5
let [a, b, ...c] = arr;
console.log(c);//[3,4,5]
function fn(...arg) {
// arguments
console.log(arg);//[1, 2, 3, 4, 5, 6, 7]
}
fn(1, 2, 3, 4, 5, 6, 7);