The most reliable way for IE 5-9 is using conditional comments:
var div = document.createElement("div"), ie;
div.innerHTML = "<!--[if IE 5]>5<![endif]--><!--[if IE 5.0]>5<![endif]-->"
+ "<!--[if IE 5.5]>5.5<![endif]--><!--[if IE 6]>6<![endif]-->"
+ "<!--[if IE 7]>7<![endif]--><!--[if IE 8]>8<![endif]-->"
+ "<!--[if IE 9]>9<![endif]-->";
if (div.firstChild && div.firstChild.nodeType === 3) ie = +div.innerHTML;
But this doesn't work for IE10-11, as those don't support conditional comments, and IE11 not even when in compatibility mode (and that kind of sucks - conditional comments have been used a lot). Edit: fortunately, that has been fixed the exact same day I gave this answer. Ah!
You can still check if it's IE10-11 if you manage to detect its vendor prefix, for example:
var prop, isIE = false;
for (prop in div.style)
if (/^ms[A-Z]/.test(prop)) {
isIE = true;
break;
}
This works but you won't be able to tell IE10 from IE11, unless you check the UA string or some recent features.
But honestly, IE10+ is fine, and should also get automatic updates. You should go for feature detection for those, and forget about the version.