Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick falsy-truthy way of checking IE or not?

I read an article about Test for Internet Explorer in JavaScript which states that a quick test is :

var isMSIE = /*@cc_on!@*/0;

if (isMSIE) {
  // do IE-specific things
} else {
  // do non IE-specific things
}

But one of the comments showed another way : ( and I have to say , it works)

if (-[1,]) {
// do non IE-specific things
} else {
// do IE-specific things
}

And so I ask :

What is so special with -[1,] that IE doesn't recognize it while others do ?

p.s.

found another quick falsy-truthy trick

IE='\v'=='v'
like image 451
Royi Namir Avatar asked Jul 03 '13 11:07

Royi Namir


1 Answers

[1,].toString() in IE prior to recent versions was 1, which when prefixed with arithmetic - would output falsey NaN whereas other browsers would return [1,].toString() === 1 for a truthy -1.

Its a horrid sniffing technique, avoid it entirely and as other have commented detect support for specific features.

like image 75
Alex K. Avatar answered Oct 02 '22 13:10

Alex K.