You can use the Categorizr JS library to test whether the user's device is a mobile phone, a tablet, a smart tv or a desktop. Categorizr uses user agent sniffing, so you should keep an eye out for updates to the script, as user agents tend to "evolve" over time.
Here's how you would use Categorizr to check if the user is on a smartphone, but not a smart TV, a desktop or a tablet:
if (categorizr.isMobile) {
//run this code if on a phone
} else {
//run this code if not on a phone
}