Community
Participate
Working Groups
Hi, Invalid warnings are issued if a local variable is declared in both branches of an if-then-else: function foo(){ if(document.getElementById('myId1')){ var myValue = document.getElementById('myId1').value;//warning never read } else{ var myValue = document.getElementById('myId2').value;//no warning ??? } //do something with myValue return myValue + 10;//warning never initialized } I know this code is ugly and variables should be declared first like that: function foo(){ var myValue = ''; if(document.getElementById('myId1')){ myValue = document.getElementById('myId1').value; } elseif(document.getElementById('myId2')){ myValue = document.getElementById('myId2').value; } else{ //do something for this really unexpected case (return or throw or whatever) } //do something with myValue return myValue + 10; } But since quick and dirty code is the defacto standard in web developpment ><, I shouldn't have any warning. Best regards, Laurent Lyaudet
Hi, the same logic applies also for try catch: function foo(){ try{ var myValue = document.getElementById('myId1').value;//warning never read } catch(exc){ var myValue = 0;//no warning ??? } //do something with myValue return myValue + 10;//warning never initialized } In that case, the "correct" code looks uglier than the incorrect one: function foo(){ var myValue = 0; try{ myValue = document.getElementById('myId1').value; } catch(exc){ //do nothing, the default value is fine } //do something with myValue return myValue + 10; } Note that I consider that a warning "Multiple declaration of local variable" would be appropriate instead of an incorrect warning "never read" or "never initialized". Best regards, Laurent Lyaudet