Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 1.18 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // This should do a pretty good job of iterating through the following array
  2. // and logging any values that == each other. Beware, this is scary stuff!
  3. // http://benalman.com/news/2010/11/schrecklichwissen-terrible-kno/
  4.  
  5. var arr = [true, 123, {}, {a:1}, [], [0], [123], "hi", function foo(){},
  6.   /re/, Infinity, false, 0, "", null, undefined, NaN];
  7.  
  8. function pretty(v) {
  9.   return /^[os]/.test(typeof v) ? JSON.stringify(v) : String(v);
  10. }
  11.  
  12. arr.forEach(function(v) {
  13.   var truthy = v ? "truthy" : "falsy";
  14.   var eq = [];
  15.   arr.forEach(function(w) {
  16.     if ( v == w ) { eq.push(pretty(w)); }
  17.   });
  18.   eq.length || eq.push("N/A");
  19.   console.log("%s (%s) == %s", pretty(v), truthy, eq.join(", "));
  20. });
  21.  
  22. /*
  23.  
  24. OUTPUT:
  25.  
  26. true (truthy) == true
  27. 123 (truthy) == 123, [123]
  28. {} (truthy) == {}
  29. {"a":1} (truthy) == {"a":1}
  30. [] (truthy) == [], false, 0, ""
  31. [0] (truthy) == [0], false, 0
  32. [123] (truthy) == 123, [123]
  33. "hi" (truthy) == "hi"
  34. function foo(){} (truthy) == function foo(){}
  35. /re/ (truthy) == /re/
  36. Infinity (truthy) == Infinity
  37. false (falsy) == [], [0], false, 0, ""
  38. 0 (falsy) == [], [0], false, 0, ""
  39. "" (falsy) == [], false, 0, ""
  40. null (falsy) == null, undefined
  41. undefined (falsy) == null, undefined
  42. NaN (falsy) == N/A
  43.  
  44. */