How to detect a mobile device in jQuery?

You can use this script to detect the screen size as you want

 

<script> 

        if (window.matchMedia("(max-width: 767px)").matches)  
        { 
            // The viewport is less than 768 pixels wide 
            console.log("This is a mobile device."); 
        } else { 
            // The viewport is at least 768 pixels wide 
            console.log("This is a tablet or desktop."); 
        } 

</script>