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");
}
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");
}
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");
}
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"