0

It is possible I am making some javascript mistakes here.

I am using a following (sample)code to add options to a drop-down list:

sel = document.getElementById("emp-list");
<for loop> (
opt = document.createElement('option');
//add value to option also add a 'assiganble' data
var assign = true;
opt.setAttribute("data-assignable", assign);
sel.appendChild(opt);
)

Now, when user select an option, I have a function that first check for 'assignable' condition.

if ($("#emp-list option:selected").data('assignable')) {
} else {
}

in one machine this works as expected ... it correctly evaluates if condition as Boolean true/false and proceed with rest of the code.

However, in a second machine it always executes 'false' block!! So far, only way to work this in second machine is to use following if condition:

if ($("#emp-list option:selected").attr('data-assignable') === "true") {

} else {

}

(if I don't use === "true" , true block is always executed; even-though i was expecting .attr('data-assignable') as boolean)

Can you explain why this behavior?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Nik
  • 153
  • 1
  • 3
  • 12
  • Possible duplicate [Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?](http://stackoverflow.com/a/359509/3648578) – kosmos May 07 '15 at 15:16
  • To answer your question, why this behavior? Is that the first method is only detecting if that attribute data-assignable exists. Not if it has a specific value. If it looks for the attribute data-assignable, and that attribute is on the element. It will return true. Because that attribute does exist. – Andrew Ice Dec 25 '16 at 05:35

2 Answers2

0

There is one concept in JavaScript called Type coercion. Type coercion means that when the operands of an operator are different types, one of them will be converted to an "equivalent" value of the other operand's type.

For example integer == double the integer value on left will be converted to double and then the two values are compared.

However, if you use ===, no such conversion occurs. When the operands are of different types, this operator returns false, and only compares the values when they're of the same type. Check more on - http://webreflection.blogspot.com/2010/10/javascript-coercion-demystified.html

Neha Narlawar
  • 229
  • 3
  • 13
  • Thanks Neha for replying. However if you look at the if condition that worked on first machine - ($("#emp-list option:selected").data('assignable') ) ; there is no Type coercion involved and I would expect this to return true or false (boolean). On second machine this was returning always false. – Nik May 07 '15 at 15:40
  • Can you remove "" (double quotes), since you want to compare boolean value rt? So why are you using string to compare your values. – Neha Narlawar May 07 '15 at 15:42
  • if I remove "" (double quotes), always else block is getting executed. – Nik May 07 '15 at 15:45
  • have u tried with === ? remove quotes and try === in your case u want to compare 2 boolean values so use === to enforce type check. – Neha Narlawar May 07 '15 at 16:20
0

I can see from your code that the data-assignable is set to true by default. So the execution of if or else blocks depends on the value of this data attribute

if($("#emp-list option:selected").data('assignable')){

  } else {

}

Are you sure that you change the data attribute later on in your code properly? I am wondering also if you have any console errors when you tried to run the same code on the second machine? Here is a simple jsfiddle, try to run the code when line 8: var assign=true and another time when var assign=false

Wael Showair
  • 3,092
  • 2
  • 22
  • 29
  • "var assign = $("#emp-list option:selected").data('assignable'); if (assign) { } else { } " is working as expected. However, "if ($("#emp-list option:selected").data('assignable')) { } else { } " is always excuting ELSE blok on 2nd machine. Strange!! What might cause this? – Nik May 11 '15 at 15:32
  • can you post your code, You did not indicate how do you change the assignable data attribute. I am really curious about the root cause of why the code worked on a machine and dont work on the second machine? Please also try to find an answer to my questions: do you have any console errors when you run the same code on the other second machine or not? – Wael Showair May 12 '15 at 04:00
  • Wael, I think I found what is causing this behavior. I added in the html file , now it is working in both machines. This is a client side application where I am loading an html page into a widget (html file local to client) . So, It is not using any browser. Widget is using an IE component. Thanks for your help!! – Nik May 12 '15 at 18:50