Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If 'if' condition is false, statements do not execute in chrome, but execute in Firefox

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.

enter image description here

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.

like image 519
Khalid Hussain Avatar asked Mar 24 '15 12:03

Khalid Hussain


People also ask

Does if false execute?

if (false) will never execute its body... because the value of the condition is never true.

How do you call a function in if else?

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.

Why do we use if else statements in Javascript?

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.


1 Answers

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)

debugger screen

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");   
}
like image 92
Patrick Evans Avatar answered Oct 04 '22 22:10

Patrick Evans