This one-liner should suffice.
var is_touch_device = 'ontouchstart' in document.documentElement;
Taken from this Stack Overflow answer here: https://stackoverflow.com/a/6447935/1585455
Your conditional should look something like this:
if(is_touch_device) { /* your code */ }