How to check if user is on handheld device (mobile/tablet) using JavaScript

Just Paste the code below and call isHandheldDevice() when you would like to know. True would mean the user is on a mobile or tablet.

<script>
function isHandheldDevice() {
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent))
return true;

return false;
}
</script>