# 1. 过滤唯一值

``````const array = [1, 1, 2, 3, 5, 5, 1]
const uniqueArray = [...new Set(array)];
console.log(uniqueArray); // Result: [1, 2, 3, 5]
``````

# 2. 在循环中缓存数组长度

``````for (let i = 0; i < array.length; i++){
console.log(i);
}
``````

``````for (let i = 0, length = array.length; i < length; i++){
console.log(i);
}
``````

# 3. 短路求值

``````x > 100 ? 'Above 100' : 'Below 100';
x > 100 ? (x > 200 ? 'Above 200' : 'Between 100-200') : 'Below 100';
``````

``````let one = 1, two = 2, three = 3;
console.log(one && two && three); // Result: 3
console.log(0 && null); // Result: 0
``````

``````let one = 1, two = 2, three = 3;
console.log(one || two || three); // Result: 1
console.log(0 || null); // Result: null
``````

``````return (foo || []).length;
``````

``````if (this.state.data) {
return this.state.data;
} else {
return 'Fetching Data';
}
``````

``````return (this.state.data || 'Fetching Data');
``````

# 4. 转换成布尔值

``````const isTrue = !0;
const isFalse = !1;
const alsoFalse = !!0;
console.log(true); // Result: true
console.log(typeof true); // Result: "boolean"
``````

# 5. 转换成字符串

``````const val = 1 + "";
console.log(val); // Result: "1"
console.log(typeof val); // Result: "string"
``````

# 6. 转换成数字

``````let int = "15";
int = +int;
console.log(int); // Result: 15
console.log(typeof int); Result: "number"
``````

``````console.log(+true); // Return: 1
console.log(+false); // Return: 0
``````

``````const int = ~~"15"
console.log(int); // Result: 15
console.log(typeof int); Result: "number"
``````

# 7. 快速幂运算

``````console.log(2 ** 3); // Result: 8
``````

``````// 以下表达式是等效的:
Math.pow(2, n);
2 << (n - 1);
2**n;
``````

# 8. 快速取整

``````console.log(23.9 | 0); // Result: 23
console.log(-23.9 | 0); // Result: -23
``````

| 的实际行为取决于操作数是正数还是负数，所以在使用这个运算符时要确保你知道操作数是正是负。

| 运算符也可以用来移除整数的尾部数字，这样就不需要像下面这样：

``````let str = "1553";
Number(str.substring(0, str.length - 1));
``````

``````console.log(1553 / 10 | 0) // Result: 155
console.log(1553 / 100 | 0) // Result: 15
console.log(1553 / 1000 | 0) // Result: 1
``````

# 9. 自动类绑定

``````import React, { Component } from React;
export default class App extends Compononent {
constructor(props) {
super(props);
this.state = {};
}
myMethod = () => {
// This method is bound implicitly!
}
render() {
return (<div>
{this.myMethod()}
</div>)
}
};
``````

# 10. 截取数组

``````let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
array.length = 4;
console.log(array); // Result: [0, 1, 2, 3]
``````

``````let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
array = array.slice(0, 4);
console.log(array); // Result: [0, 1, 2, 3]
``````

# 11. 获取数组最后的元素

``````let array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(array.slice(-1)); // Result: [9]
console.log(array.slice(-2)); // Result: [8, 9]
console.log(array.slice(-3)); // Result: [7, 8, 9]
``````

# 12. 格式化 JSON

stringify() 方法可以接受两个额外的参数，一个是函数（形参为 replacer），用于过滤要显示的 JSON，另一个是空格个数（形参为 space）。

space 可以是一个整数，表示空格的个数，也可以是一个字符串（比如’ ’表示制表符），这样得到的 JSON 更容易阅读。

``````console.log(JSON.stringify({ alpha: 'A', beta: 'B' }, null, '   '));
// Result:
// '{
//  "alpha": A,
//  "beta": B
// }'
``````