How to detect Mobile device using JavaScript

Here is how to detect if user has opened site in mobile device or desktop. For this we're using JavaScript code which will be executed at client-side.