One JS little knowledge point three practical javascript tips every day
- Get array elements from back to front
var arr = [1, 2, 3, 4]
console.log(arr.slice(-1)) // [4]
console.log(arr.slice(-2)) // [3, 4]
console.log(arr.slice(-3)) // [2, 3, 4]
console.log(arr.slice(-4)) // [1, 2, 3, 4]
- Short-circuit conditional sentence
If you want to execute a certain function when a certain conditional logic value is true, such as
if (condition) {
dosomething()
}
The short circuit can be used like this:
condition && dosomething()
- Use the operator "||" to set the default value
var a
console.log(a) // undefined
a = a || '默认值'
console.log(a) // 默认值
a = a || 'new value'
console.log(a) // 默认值