// badif (test1!==null||test1!==undefined||test1!=="") {
lettest2=test1;
}
// betterlettest2=test1||"";
// badif (test1===true) orif (test1!=="") orif (test1!==null)
// betterif (test1){
// do some
}else{
// do other
}
//Note: If test1 has a value, the logic after if will be executed. //This operator is mainly used for null, undefined, and empty string checks.
// badconstdata="hello maxwell this is a test\n\t"+"test test,test test test test\n\t";
// betterconstdata=`hello maxwell this is a testtest test,test test test test`;
// badif (arr.indexOf(item) >-1) {
// item found
}
if (arr.indexOf(item) ===-1) {
// item not found
}
// betterif (~arr.indexOf(item)) {
// item found
}
if (!~arr.indexOf(item)) {
// item not found
}
//The bitwise (~) operator will return true (except for -1), //the reverse operation only requires !~. Alternatively, the includes() function can be used.if (arr.includes(item)) {
// true if the item found
}
asyncfunctiongetData() {
constpromises= [fetch("url1"), fetch("url2"), fetch("url3"), fetch("url4")];
for (constitemofpromises) {
// Print out the promiseconsole.log(item);
}
// betterforawait (constitemofpromises) {
// Print out the results of the requestconsole.log(item);
}
}
constpromises= [fetch("index.html"), fetch("https://does-not-exist/")];
constresults=awaitPromise.allSettled(promises);
// Filter out successful requestsconstsuccessfulPromises=results.filter((p) =>p.status==="fulfilled");
// Filter out failed requests and output the reasonconsterrors=results
.filter((p) =>p.status==="rejected")
.map((p) =>p.reason);
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
19.交换数组元素的位置
// badconstswapWay= (arr, i, j) => {
constnewArr= [...arr];
lettemp=newArr[i];
newArr[i] =list[j];
newArr[j] =temp;
returnnewArr;
};
//Since ES6, swapping values from different locations in an array has become much easier// betterconstswapWay= (arr, i, j) => {
constnewArr= [...arr];
const [newArr[j],newArr[i]] = [newArr[i],newArr[j]];
returnnewArr;
};