One JS little knowledge point three practical javascript tips every day

One JS little knowledge point three practical javascript tips every day

  1. 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]
  1. 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()
  1. 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) // 默认值

Guess you like

Origin blog.csdn.net/weixin_43881166/article/details/115200229