JavaScript 技巧和窍门。

2025-05-25

JavaScript 技巧和窍门。

!! operator

要检查一个值是真还是假,!! operator你可以调用这个双重非或非非运算符,你也可以在这里使用布尔函数。

console.log(!! 0)             // output: false
console.log(!! 1)            // output: true

console.log(Boolean(1))     // output: true
console.log(Boolean(0))    // output: false
Enter fullscreen mode Exit fullscreen mode

转变string → number

将字符串转换为数字

const string = '101'

console.log(+string)          // output: 101
console.log(Number(string))   // output: 101
Enter fullscreen mode Exit fullscreen mode

reverse method

使用反向方法来反转数组项的顺序,注意反向方法会改变原始数组。

const numbers = ['1', '2', '3']

console.log(numbers.reverse())   // output: [ "3", "2", "1" ]
Enter fullscreen mode Exit fullscreen mode

Math.min & max

使用 Math.min 和 Math.max 函数从数组中查找最小值或最大值。

const numbers = [1, 2 ,3, 4, 5]

console.log(Math.min(...numbers)) // output: 1

console.log(Math.max(...numbers)) // output: 5
Enter fullscreen mode Exit fullscreen mode

合并Arrays

使用扩展运算符合并数组。

const fruits = ['🍎', '🍌']

const vegetables = ['🥔', '🥕']

const food = [...fruits, ...vegetables]

console.log(food) // output: [ "🍎", "🍌" , "🥔", "🥕" ]
Enter fullscreen mode Exit fullscreen mode

falsey values

在 javascript 中有九个 falsey 值。

undefined , null , NaN , 0 , 0n (BigInt 0), -0 ""(empty string),false,document.all
Enter fullscreen mode Exit fullscreen mode

ternary operator

三元运算符允许您以更紧凑的方式编写 if...else 语句。

let number = 1

if (number == 1) {
  console.log('number is one')
} else {
  console.log('number is not one')
}

// Syntax: condition ? exprIfTrue : exprIfFalse  (MDN)

console.log(number === 1 ? "number is one" : "number is not one");

Enter fullscreen mode Exit fullscreen mode

消除duplicates from array

const fruits = ['🍎', '🍊', '🍎', '🍊']

// Method 1:
const filteredFruits = Array.from(new Set(fruits))
console.log(filteredFruits) // output: Array [ "🍎", "🍊" ]

// Method 2:
const filteredFruits = [...new Set(fruits)]  
console.log(filteredFruits) // output: Array [ "🍎", "🍊" ]
Enter fullscreen mode Exit fullscreen mode

map method

如果要操作数组项,请尝试使用 map 方法,map 方法对数组的每个元素执行给定的函数,并根据原始数组返回一个新数组

const numbers = [1, 2, 3, 4, 5]

const mapedNumbers = numbers.map(element => element + 1) 

console.log(mapedNumbers) // output: [2, 3, 4, 5, 6]
Enter fullscreen mode Exit fullscreen mode

includes method

要检查数组是否包含某个值,请使用 includes 方法。

const hearts = ['🧡', '💙', '🤍']

console.log(hearts.includes('🧡'))  // output: true

console.log(hearts.includes('❤️'))  // output: false
Enter fullscreen mode Exit fullscreen mode

filter method

根据条件过滤数组过滤方法以函数作为参数,对数组的每个元素执行该函数并返回新数组。

const numbers = [1, 5, 6, 7, 4]

const filteredArray = numbers.filter(element => element > 4)

console.log(filteredArray)  // output: [ 5, 6, 7 ]
Enter fullscreen mode Exit fullscreen mode

滚动到顶部button

const button  = document.querySelector('button')

button.addEventListener('click', function () {
  window.scrollTo(0,0)
})

Enter fullscreen mode Exit fullscreen mode

点击按钮“移动到顶部”后,页面从底部滚动到顶部 gif

编码快乐😊

文章来源:https://dev.to/devsyedmohsin/javascript-tips-and-tricks-2mhk
PREV
利用开源软件构建更健康的地球 开放可持续技术 CASSIE – 通过卫星图像引擎进行海岸分析
NEXT
React 最佳实践和模式以减少代码 - 第 2 部分