Detect if device is tablet

How can I detect if the device is ONLY tablet? This script below is not working properly.

var ua = navigator.userAgent, tablet = /Tablet|iPad/i.test(ua);
alert("Tablet? " + tablet);

I don't want check if is mobile and use else to show tablet. I want only a function to confirm if is tablet. How can I do that? Thanks


If you want to just use vanilla javascript you can use the navigator API

const userAgent = navigator.userAgent.toLowerCase();
const isTablet = /(ipad|tablet|(android(?!.*mobile))|(windows(?!.*phone)(.*touch))|kindle|playbook|silk|(puffin(?!.*(IP|AP|WP))))/.test(userAgent);

But i'd suggest using a librray such as

if (WURFL.is_mobile === true && WURFL.form_factor === "Tablet") {
    // targetSmartPhoneDevices();