This is the plnkr of my scenario. When I debug this code in chrome, statement in second 'if' is not executed. But when I debug it in Firefox, the statement in second 'if' is executed.
angular.module('optionsExample', [])
.controller('ExampleController', ['$scope', function($scope) {
debugger;
if(true){
$scope.name = 'Foo';
}
if(false){
$scope.name = 'lol';
}
}]);
Firefox version: 35.0.1 Chrome version: 41.0.2272.101 m
Why does it happen?
Update:
Also tested in Firefox 36.0.4, Same problem.
if (false) will never execute its body... because the value of the condition is never true.
Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. Use switch to select one of many blocks of code to be executed.
It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
This maybe a bug or it may just be how they designed it. But this seems to do with how the debugger deals with if statement conditions it can readily evaluate, and the code styling used (K&R, Allman, 1TBS, etc)
Lines of code that can have breakpoints set to them are shaded green, and ones that cannot are shaded gray by the debugger.
Simply put the debugger does not highlight if statement lines (and maybe even other control statement lines) that it can readily tell have a true/false condition. When setting a breakpoint the breakpoint will be put on the next breakable line of code instead.
In the case of blocks that use 1TBS/OTBS or K&R style formatting the debugger will highlight the first breakable line within the if block instead of the actual if statement line. This gives the false impression that the code inside false condition if statements are going to be executed next.
In the case of blocks that use Allman style formatting the debugger will not highlight any part of the if statement or code block when stepping through. And if you try to put a code break on the if statement it will skip the entire block and put the breakpoint on the next breakable line.
When the if statements do not use conditions that the debugger can readily read as true/false the debugger acts as expected.
Demo to test
debugger;
var one = true;
var two = false;
//Allman Style
if(one)
{
console.log("Truth");
}
if(two)
{
console.log("false");
}
if(true)
{
console.log("Truth");
}
if(false)
{
console.log("false");
}
//1TBS/OTBS Style
if(one){
console.log("Truth");
}
if(two){
console.log("false");
}
if(true){
console.log("Truth");
}
if(false){
console.log("false");
}
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