Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Bitwise IndexOf Shorthand in javascript

// Bitwise IndexOf Shorthand in javascript
// Longhand:
if(arr.indexOf(item) > -1) { 
  // Confirm item IS found
}

if(arr.indexOf(item) === -1) { 
  // Confirm item IS NOT found
}

// Shorthand:
if(~arr.indexOf(item)) { 
  // Confirm item IS found
}

if(!~arr.indexOf(item)) { 
  // Confirm item IS NOT found
}

// we can also use the includes() function:
if(arr.includes(item)) { 
  // Returns true if the item exists, false if it doesn't
}
 
PREVIOUS NEXT
Tagged: #Bitwise #IndexOf #Shorthand #javascript
ADD COMMENT
Topic
Name
7+4 =