Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to compare two strings where select and value are different types?

Tags:

jquery

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?

like image 390
ei.schinken Avatar asked Nov 28 '22 17:11

ei.schinken


1 Answers

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.

like image 107
mas-designs Avatar answered Dec 23 '22 03:12

mas-designs