1.?替换if...else语句constage=12;letadultOrChild;if(age>=18){adultOrChild='Adult';}else{adultOrChild='Child';}//速记adultOrChild=age>=18?'Adult':'Child';/***函数大小写*/if(age>=18){doAdultThing();}else{doChildThing();}//shorthand(age>=18?doAdultThing:doChildThing)();2。&&短路替换if语句constage=18;letadultOrChild;if(age>=18){adultOrChild='Adult';}//速记age>=18&&(adultOrChild='Adult');3.包括压缩多个if语句constnum=3;if(num===1||num===2||num===3){console.log('OK');}//shorthandif([1,2,3].includes(num)){console.log('OK');}//也可以用&&进一步简写[1,2,3].includes(num)&&console.log('好的');4。用对象替换if...else...if语句constname='Tom';letjudge='';if(name='Jack'){judge='good';}elseif(name='John'){judge='normal';}elseif(name='Tom'){judge='bad';}//ShorthandconstjudgeObj={Jack:'good',John:'normal',Tom:'bad'}letjudge=judgeObj[name];5.**指数运算Math.pow(6,3);//216//简写6**3;//2166.~~实现向下取WholeMath.floor(6.3);//6//简写~~6.3//67.!!将值转换为布尔类型!!'test'//true!!true//true!!3//true!![]//真8。使用+将字符串转为数字Number('89')//89+'89'//89//如果字符串中包含非数字字符,则变为NaN+'3A96'//NaN9。?。防崩溃处理constperson={name:'Jack',age:18,otherInfo:{height:178}}console.log(person&&person.otherInfo&&person.otherInfo.height);//简写console.log(人?.otherInfo?.height);//?是ES11中的内容,目前Chrome浏览器控制台不支持10。使用??判断null和undefinedconstcount=0;//当count为null时打印-当undefined和undefined时,否则打印countif(count===null||count===undefined){console.log('-');}else{console.log(count);}//缩写控制台。日志(计数??'-');11.使用展开运算符...//复制数组的场景constarr=[1,2,3,4];constcopyArr1=arr.slice();constcopyArr2=[...arr];//合并数组的场景constnewArr=[5,6,7,8];constmergeArr1=arr.concat(newArr);constmergeArr2=[...arr,...newArr];//对象同理constperson={name:'Jack',age:18}constnewPerson={...person,height:180}12、使用Set对数组进行去重constarr=[1,1,2,3,4,4];constuniqueArr=[...newSet(arr)];13.用一行代码给多个变量赋值leta,b,c,d;a=1;b=2;c=3;d=4;//简写leta=1,b=2,c=3,d=4;//或使用解构赋值[a,b,c,d]=[1,2,3,4];js对象使用解构赋值:letstudent={name:'Jack',age:18}letname=student.name;letage=student.age;//简写let{name,age}=student;14.交换两个变量的值letx=1;lety=2;lettemp=x;x=y;y=temp;//简写[x,y]=[y,x];15。使用Array.find()从数组中查找特定元素constpersonList=[{name:'Jack',age:16},{name:'Tom',age:18},]letJackObj={};for(让i=0,len=personList.length;i
