-2

I needed to iterate thought object and I'm losing my mind WIth this.

var obj = { a:"here", b: ["here"]};
for(var o in obj){
  alert(obj[o]=="here");
  }
Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80

2 Answers2

2

The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. It's this case where === will be faster, and may return a different result than ==. In all other cases performance will be the same.

It should be using === instead of ==:

var obj = { a:"here", b: ["here"]};
for(var o in obj){
  alert(obj[o]==="here");
  }
huan feng
  • 7,307
  • 2
  • 32
  • 56
2

That's because you are comparing a string with an array by using the == operator. JavaScript interpreter converts the array into a string by calling the Array.prototype.toString method. The method calls the Array.prototype.join method behind the scenes.

["here"].toString() // => "here"
["here", "foo"].toString() // => "here,foo"
Ram
  • 143,282
  • 16
  • 168
  • 197