I'm having some problems to compare two strings in jQuery:
var option = "";
$.each(data, function(key, value) {
option += "<option ";
if(selected == value) {
option += "selected";
}
option += ">" + value + "</option>";
});
selected and value are the same strings.
Is there an another way to compare two strings or could it be, that selected and value are different types?
You have to consider this:
The == operator compares two values and returns true if the values on both sides are equal to one another. If the two values have different data types (for example, if one is a number and the other is a text string) then they are both converted to the same type before the comparison takes place. JavaScript will convert whichever of the two types can be converted to the other type without having to change the value contained in the variable (for example, converting a number to a text string).
The === operator does the same thing with one minor difference. This operator does not convert data from one type to another. It only returns true when the variables being compared are both of the same type and contain the same value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With