# 想让你的代码变得更加优雅吗？

1. 多个条件判断

// longif( x === 'a' || x === 'b' || x === 'c' || x === 'd'){  // todo}// shortif(['a', 'b', 'c', 'd'].includes(x)){  // todo}

2. 三目运算符

// longlet flagif(x > 10){  flag = true}else {  flag = false}// shortlet flag = x > 10 ? true : false

3. 变量声明

// longlet alet b = 1// shortlet a, b = 1

4. 空/未定义检查和分配默认值

undefined
, 可以考虑如下实现:

// longif(test1 !== null || test1 !== undefined || test1 !== ""){let test2 = test1;}else {let test2 = ''}// shortlet test2 = test1 || ''

5. 给多个变量赋值

//long let test1, test2, test3;test1 = 1;test2 = 2;test3 = 3;//Short let [test1, test2, test3] = [1, 2, 3];

6. 赋值运算符的简写

// longtest1 = test1 + 1;test2 = test2 - 1;test3 = test3 * 20;// shorttest1++;test2--;test3 *= 20;

7. 真值判断

// longif (test1 === true)// shortif (test1)

8. 多条件的与/或运算

//long if (test1) { callMethod(); } //short test1 && callMethod();

9. forEath

// longfor (var i = 0; i < testList.length; i++)// shorttestList.forEach(item => console.log(item))

10. 比较返回值

// longlet test;function checkReturn() {if (!(test === undefined)) {return test;    } else {return callMe('test');    }}var data = checkReturn();console.log(data); //output testfunction callMe(val) {    console.log(val);}// shortfunction checkReturn() {return test || callMe('test');}

11. 箭头函数

//long function add(a, b) { return a + b; } //short const add = (a, b) => a + b;

12. 短函数调用

// longfunction test1() {  console.log('test1');};function test2() {  console.log('test2');};var test3 = 1;if (test3 == 1) {  test1();} else {  test2();}// short(test3 === 1? test1:test2)();

13. switch

// longswitch (data) {case 1:    test1();break;case 2:    test2();break;case 3:test();break;  // And so on...}// shortvar data = {  1: test1,  2: test2,  3: test};data[something] && data[something]();

14. 默认参数

//longfunction add(test1, test2) {if (test1 === undefined)    test1 = 1;if (test2 === undefined)    test2 = 2;return test1 + test2;}//shortadd = (test1 = 1, test2 = 2) => (test1 + test2);add() //output: 3

15. 参数必传校验

// longfunction hello(obj){let {name, age} = objif(!name){    console.warn('name is null, pls check!')return ''  }if(!age){    console.warn('age is null, pls check!')return ''  }return ${name}:${age}}// shortfunction hello(obj){let {name = required('name'), age = required('age')} = objreturn ${name}:${age}}function required(key){  console.warn(${key} is null, pls check!')} 16. 扩展运算符 //longconst data = [1, 2, 3];const test = [4 ,5 , 6].concat(data);//shortconst data = [1, 2, 3];const test = [4 ,5 , 6, ...data];console.log(test); // [ 4, 5, 6, 1, 2, 3] 对于克隆, 我们也可以使用扩展运算符 //longconst test1 = [1, 2, 3];const test2 = test1.slice()//shortconst test1 = [1, 2, 3];const test2 = [...test1]; 17. 模板字符串 如果您厌倦了在单个字符串中使用+来连接多个变量，可以考虑用这种方式 //longconst welcome = 'Hi ' + user + ' ' + name + '.'//shortconst welcome = Hi${user} \${name};

18. 对象属性赋值

let test1 = 'a'; let test2 = 'b';//Longlet obj = {test1: test1, test2: test2}; //short let obj = {test1, test2};

19. 字符串转换成数字

//Longlet test1 = parseInt('123'); let test2 = parseFloat('12.3'); //Shortlet test1 = +'123'; let test2 = +'12.3';

20. Array.find

const data = [{type: 'test1',        name: 'abc'    },    {type: 'test2',        name: 'cde'    },    {type: 'test1',        name: 'fgh'    },]// longfunction findtest1(name) {for (let i = 0; i < data.length; ++i) {if (data[i].type === 'test1' && data[i].name === name) {return data[i];        }    }}//shorthandfilteredData = data.find(data => data.type === 'test1' && data.name === 'fgh');console.log(filteredData); 

21. 多条件判断

// longif (type === 'test1') {  test1();}else if (type === 'test2') {  test2();}else if (type === 'test3') {  test3();}else if (type === 'test4') {  test4();} else {  throw new Error('Invalid value ' + type);}// shortconst types = {  test1: test1,  test2: test2,  test3: test3,  test4: test4};let func = types[type];(!func) && throw new Error('Invalid value ' + type); func();

22. 索引查找

//longif(arr.indexOf(item) > -1) { // item found }if(arr.indexOf(item) === -1) { // item not found}//shortif(~arr.indexOf(item)) { // item found}if(!~arr.indexOf(item)) { // item not found}

按位~运算符

if (arr.includes(item)) { // true if the item found}

23. Object.entries()

const data = { test1: 'abc', test2: 'cde', test3: 'efg' };const arr = Object.entries(data);console.log(arr);/** ouput[ [ 'test1', 'abc' ],  [ 'test2', 'cde' ],  [ 'test3', 'efg' ]]**/

24. Object.values()

const data = { test1: 'abc', test2: 'cde' };const arr = Object.values(data);console.log(arr);/** Output:[ 'abc', 'cde']**/

25. 重复一个字符串多次

//long let test = ''; for(let i = 0; i < 5; i ++) { test += 'test '; } console.log(str); // test test test test test //short 'test '.repeat(5);

26. 在数组中查找最大值和最小值

const arr = [1, 2, 3]; Math.max(…arr); // 3Math.min(…arr); // 1`