My Easy Mistake Lists

Easy forget indexOf return a number, not a boolean

  • exist -> return the first index of the element
  • not exist -> return -1
  // if exist
if(arr.indexOf('something' !== -1)){
}else{
  // if not exist
}

the code above is equal to below

if(~arr.indexOf('something')){
}else{
}

你可能感兴趣的:(My Easy Mistake Lists)