There’s a CSS media just for that!
You can check whether some device has a mouse by getting the value of the pointer
CSS media feature:
if (matchMedia('(pointer:fine)').matches) {
// Device has a mouse
}
Because it’s CSS you don’t even need to use JavaScript:
@media (pointer: fine) {
/* Rules for devices with mouse here */
}