逻辑或操作符||

如果||前面的值是0 '' false null undefined NaN其中的任意一种,则直接返回||后面的值

function(obj){
var a = obj || {}
}
// 等价于 =>>
function(obj){
var a;
if(
obj === 0 ||
obj === "" ||
obj === false ||
obj === null ||
obj === undefined ||
isNaN(obj)
){
a = {}
} else {
a = obj;
}
}

空值合并操作符??

如果??前面的值没有定义返回后面。如果定义,则返回前面;有时不想把空字符串或者0也当做false处理可以使用此法

function(obj){
var a = obj ?? {}
}
// 等价于 =>>
function(obj){
var a;
if(
obj === null ||
obj === undefined
){
a = {}
} else {
a = obj;
}
}

// 不想把0当成false可以用此方法
if(value !== null && value !== undefined && value !== ''){}
// 等价于 ==>
if((value ?? '') !== ''){}

防止崩溃的可选链(?.)

1、可选链操作符?. 如果访问未定义的属性,则会产生错误。在未定义属性时使用可选链运算符,undefined将返回而不是错误。这可以防止你的代码崩溃。 2、可选链运算符也可以用于方法调用。如果方法存在,它将被调用,否则将返回 undefined

// 1、对象
const student = {
name: "lwl",
address: {
state: "New York"
},
}

// 一层一层判断
console.log(student && student.address && student.address.ZIPCode) // 输出:undefined
// 使用可选链操作符
console.log(student?.address?.ZIPCode) // 输出:undefined

// 2、方法
const obj = {
foo() {
console.log('Hello from foo!')
}
}

obj.foo?.() // 输出:'Hello from foo!'
obj.bar?.() // 输出:undefined,因为 bar 方法不存在

// 3.数组
const arr = [1, 2, 3, 4]

console.log(arr[0]) // 输出:1
console.log(arr[4]) // 输出:undefined

// 使用可选链运算符
console.log(arr?.[0]) // 输出:1
console.log(arr?.[4]) // 输出:undefined
console.log(arr?.[0]?.toString()) // 输出:'1'

逻辑空赋值(??=)

逻辑空赋值??= 逻辑空赋值运算符(x ??= y)仅在 x 是 nullish (null 或 undefined) 时对其赋值。

const a = { duration: 50 };

a.duration ??= 10;
console.log(a.duration);
// expected output: 50

a.speed ??= 25;
console.log(a.speed);
// expected output: 25

快速生成1-10的数组

注意: 二维数组不能直接写成new Array(10).fill([])(也就是fill方法不能传引用类型的值,[]换成new Array()也不行),因为fill里传入引用类型值会导致每一个数组都指向同一个地址,改变一个数据的时候其他数据也会随之改变

// 生成0-9,利用了数组的下标值
const arr1 = [...new Array(10).keys()]
const arr2 = Array.from(Array(10), (v, k) => k)

// 生成1-10,通过map的特性
const arr3 = [...Array(10)].map((v, i) => i + 1)

// 快速生成10个0的数组
const arr4 = new Array(10).fill(0)

// 快速生成10个[]的数组(二维数组)
// 错误写法
const arr = new Array(10).fill([]) // 注意这是错误写法,不要这么写
// 正确写法
const arr = new Array(10).fill().map(() => new Array())

数组降维

如果不确定需要降维的数组有多深,可以传入最大值作为参数Infinity,默认值深度为1

const arr = [1, [2, [3, 4], 5], 6]
const flatArr = arr.flat(Infinity) // 输出 [1, 2, 3, 4, 5, 6]

const arr = [1, 2, 3, 4]
const result1 = arr.map(v => [v, v * 2]).flat()
const result2 = arr.flatMap(v => [v, v * 2])
console.log(result1, result2); // 输出 [1, 2, 2, 4, 3, 6, 4, 8]

从数组中删除重复项

在 JavaScript 中,Set 是一个集合,它允许你仅存储唯一值。这意味着删除任何重复的值

const numbers = [1, 1, 20, 3, 3, 3, 9, 9];
const uniqueNumbers = [...new Set(numbers)]; // -> [1, 20, 3, 9]

在没有第三个变量的情况下交换两个变量

在 JavaScript 中,你可以使用解构从数组中拆分值

let x = 1;
let y = 2;

// 交换变量
[x, y] = [y, x];

指数运算符

Math.pow(2, 3); // 输出: 8 
2 ** 3; // 输出: 8

Math.pow(4, 0.5); // 输出: 2
4 ** 0.5; // 输出: 2

Math.pow(3, -2); // 输出: 0.1111111111111111
3 ** -2; // 输出: 0.1111111111111111

Math.pow('2', '3'); // 输出: 8 (参数被自动转换为数字)
'2' ** '3'; // js中输出: 8,其他语言可能报错

Math.floor() 简写

对于正数而言两者都是直接去掉小数位,但对于负数来说Math.floor()是向下取整,~~是只去掉小数位,整数位不变

Math.floor(3.14); // 输出: 3 
Math.floor(5.7); // 输出: 5
Math.floor(-2.5); // 输出: -3
Math.floor(10); // 输出: 10

~~3.14; // 输出: 3
~~5.7; // 输出: 5
~~(-2.5); // 输出: -2
~~10; // 输出: 10

逗号运算符(,)

逗号( , )运算符对它的每个操作数从左到右求值,并返回最后一个操作数的值。这让你可以创建一个复合表达式,其中多个表达式被评估,复合表达式的最终值是其成员表达式中最右边的值。这通常用于为 for 循环提供多个参数。

// 简化前
const result = arr => {
arr.push('a')
return arr
}
console.log(result([1,2])) // 输出:[1, 2, 'a']

// 简化后
const result = arr => (arr.push('a'), arr)
console.log(result([1,2])) // 输出:[1, 2, 'a']

Array.map()的简写

// 接口返回数据
res = [{
id: 1,
name: 'zhangsan',
age: 16,
gender: 0
}, {
id: 2,
name: 'lisi',
age: 20,
gender: 1
}]

// 第一种方法:箭头函数、 解构赋值
const data = res.map(({id, name}) => ({id, name}))
// 第二种方法:箭头函数、返回对象(相对更容易理解)
const data = res.map(v => ({id: v.id, name: v.name}))