1. 用数组元素的值来声明变量和给变量赋值

let a = foo[0], b = foo[1];
    改成:
    let [a, b] = foo;

2. 将逻辑运算符用于简单的条件语句中

if (foo) {
    doSomething();
  }
  改成:
  foo && doSomething();

3. 根据条件给函数传参

if(!foo){
    foo = 'apple';
  }
  bar(foo, kip);
  改为:
  bar(foo || 'apple', kip);

4. 声明大数字(很多0)的变量

const SALARY = 150000000,
    TAX = 15000000;
    改为:
    const SALARY = 15e7,
    TAX = 15e6;

5. 一个调试小技巧

const { ['log']: c } = console;
    c("something");

6. 生成带有随机数的列表

Array.from({ length: 1000 }, Math.random)
    // [ 0.6163093133259432, 0.8877401276499153, 0.4094354756035987, ...] - 1000 items

7. 生成带有数字的列表

Array.from({ length: 1000 }, (v, i) => i)
    // [0, 1, 2, 3, 4, 5, 6....999]

8. 将颜色的RGB值转换成十六进制值

const rgb2hex = ([r, g, b]) =>
    `#${(1 << 24) + (r << 16) + (g << 8) + b}`.toString(16).substr(1);

    rgb2hex([76, 11, 181]);
    // #4c0bb5

9. 将颜色的十六进制值转换成RGB值

const hex2rgb = hex =>
    [1, 3, 5].map((h) => parseInt(hex.substring(h, h + 2), 16));
  
  hex2rgb("#4c0bb5");
  // [76, 11, 181]

10. 显示N分钟前

const fromAgo = (date) => {
    const ms = Date.now() - date.getTime();
    const seconds = Math.round(ms / 1000);
    const minutes = Math.round(ms / 60000);
    const hours = Math.round(ms / 3600000);
    const days = Math.round(ms / 86400000);
    const months = Math.round(ms / 2592000000);
    const years = Math.round(ms / 31104000000);
  
    switch (true) {
      case seconds < 60:
        return `${seconds} second(s) ago"`;
      case minutes < 60:
        return `${minutes} minute(s) ago"`;
      case hours < 24:
        return `${hours} hour(s) ago"`;
      case days < 30:
        return `${days} day(s) ago`;
      case months < 12:
        return `${months} month(s) ago`;
      default:
        return `${years} year(s) ago`;
    }
  };
  
  const createdAt = new Date(2021, 0, 5);
  fromAgo(createdAt); // 14 day(s) ago;

11. 获取url参数

function getRequest(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
    var r = window.location.search.substr(1).match(reg);
    if (r!=null) return unescape(r[2]); return null;
 }
 
 // /index?id=1
 getRequest(id);
 // 1

12. 多表达式多 if 判断

// 长
    if (x === 'abc' || x === 'def' || x === 'ghi' || x ==='jkl') {
      //logic
    }
    // 短
    if (['abc', 'def', 'ghi', 'jkl'].includes(x)) {
      //logic
    }

13. 短函数调用

const fun1 = () => console.log('fun1');
    const fun2 = () => console.log('fun2');
    // 长
    let test = 1;
    if (test == 1) {
      fun1();
    } else{
      fun2();
    }
    // 短
    (test === 1? fun1:fun2)();

14. 数组合并和复制

// 长-合并数组
    const data = [1, 2, 3];
    const test = [4 ,5 , 6].concat(data);
    // 短-合并数组
    const data = [1, 2, 3];
    const test = [4 ,5 , 6, ...data];

    // 长-拷贝数组
    const test1 = [1, 2, 3];
    const test2 = test1.slice()
    // 短-拷贝数组
    const test1 = [1, 2, 3];
    const test2 = [...test1];

15. 模版字符串

// 长
    const welcome = 'Hi ' + test1 + ' ' + test2 + '.'
    // 短
    const welcome = `Hi ${test1} ${test2}`;

16. 简写对象

let test1 = 'a'; 
    let test2 = 'b';
    // 长 
    let obj = {test1: test1, test2: test2}; 
    // 短 
    let obj = {test1, test2};

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

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