Wednesday, 15 January 2014

javascript - Why did my incorrect if statement work? -


I recently came to the following syntax error:

  if (button == Of course this should be:  
  if (button == "init" | button = "init" "Show_selected"} "show_all") {  

= "Show_selected" || button == "show_all") {

But, the original statement seemed to work perfectly in Chrome, FF and IE 9 !!? I was just concerned about my mistake when adding a new option.

To clarify, string logic is used when calling "in_select", "show_selected" and "show_all" function; E.g.

  onclick = myFunction ("init");  

I'm sure that I miss early while trying to get such a short story when learning JS and knowing very quickly that it does no work.

I have already corrected the code, but I am worried that I do not know why this was working.

Can someone shed light on this puzzle?

Of course this will work, and it will always work because your status < Strong> always correct :

  if (button == "init" || "show_selected" || "show_all")  < / Pre> 

will always look true because "show_selected" is a string and if you pass it as a statement statement it will always be true, your code will be evaluated this way

  if (button == "init" || true || true) // will always be true  

Because Write ("show_all") is equal to ("show_all"! == zero) which is true .

For example, try:

  if (" show_all ") {// return true (statement is true) alert (true); }  


No comments:

Post a Comment