How to detect for Mobile device Programmatically in Magento 2?

In Magento 2 you can detect mobile device by below code snippets.

Sometimes you need to display different data on mobile device specific or hide some data from the Mobile device at that time you have useful below code snippet.

    $userAgent = $this->getRequest()->getHeader('useragent');
    $server = $this->getRequest()->getServer();
    $isMobileDevice = \Zend_Http_UserAgent_Mobile::match($userAgent, $server);
    if($isMobileDevice) {
        //CODE FOR MOBILE DEVICE
    }