So this was a fun issue I ran in to today. I had a list that allowed users to drag and drop items or double click to add a component to the bottom of the list. The code looked something like this:

addItems(item, index){
 if(index){
   // Splice in to correct position
 } else{
   // Push to the bottom
 }
}

The thinking here is that if the user had dragged and dropped the element we would provide this function with the index position of where the element was dropped. If that index was not provided then we would push the value to the bottom of the list.

This is all fine and dandy until you consider what if you have an index of zero. Zero to Javascript is both the start index of an array, and also a falsely value when you check it in an if statement. So what do we do.

Well for this example, where the presence of an argument dictates functionality we can use the undefined keyword to resolve our issue

if(index !== undefined){
 // Splice in to correct position
} 

Now this if statement reads, IF the variable index IS NOT the value of undefined THEN run the functionality in the block