Date
let now = new Date();
let allFives = new Date(2005,4,5,17,55,55);
console.log(allFives);
console.log(now.toDateString());
console.log(now.getFullYear());
console.log(now.getMonth());
console.log(now.getDate());
console.log(now.getDay());
Boolean(不使用!!!)
Number
let num = 10.005;
console.log(num.toFixed(2));
console.log(num.toExponential(1))
String
let str = "hello";
let result = str.concat(" world");
let result2 = str + " world";
console.log(result);
console.log(result2);
let str = "hello world";
console.log(str.slice(3));
console.log(str.substr(3));
console.log(str.substring(3));
console.log(str.slice(3,7));
console.log(str.substr(3,7));
console.log(str.substring(3,7));
三个方法传一个参数表示子字符串开始的位置 slice()和substring()第二个参数表示子字符串结束的位置 substr()第二个参数表示截取字符串的长度
let str = "hello world";
console.log(str.indexOf("o"));
console.log(str.lastIndexOf("o"));
console.log(str.indexOf("o",6));
console.log(str.lastIndexOf("o",6));
console.log(str.includes("wor"))
indexOf()和lastIndexOf()都是查找字符串位置的方法,区别在于从头开始和从末尾开始 如果找不到字符串则返回-1 第二个参数表示从哪个位置开始寻找 includes()遍历是否存在字符串返回布尔值
let str = " hello world ";
let str2 = str.trim()
console.log(str);
console.log(str2);
trim()方法删除字符串前后空格,trimLeft()和trimRight()以此类推
let str = "hello";
let str2 = str.repeat(3);
console.log(str2);
参数为复制几次
let msg = "abcde";
let msgArr = [];
for(const c of msg){
msgArr.push(c)
}
console.log(msgArr);
let msg = "hello WORLD";
console.log(msg.toUpperCase());
console.log(msg.toLowerCase())
|