Your original post didn't explain why you wanted to check the userAgent, so I wasn't sure what you were doing. It's up to you if you prefer to serve a dedicated version of your site to mobile users, and if that's what you decide to do then checking the userAgent string is your only choice. But it's best to handle the redirection at the server level, rather than relying on JavaScript. If you use JavaScript, the user has to load the entire default HTML document first, and some additional files will probably start to be downloaded before that JavaScript gets executed, which wastes the user's time and bandwidth. Some Google searches will show you how to do this at the server level.
Not every design works well on mobile devices, but you'd be surprised by how often responsive design actually can be used effectively.