-1

Could anyone explain me why it displays the alert dialog while the didSolve variable is false. @_@

            var didSolve = false;  

            $.get(
                "http://stardebris.net/solve-field.php",
                {id : "<?php echo $_GET['id']; ?>" },
                function(data) 
                {
                      didSolve = true;
                }
            });           
            var img = new Image();
            var div = document.getElementById('image-gallery');

            img.onload = function() 
            {
                div.appendChild(img);
                $('.pannable-image').ImageViewer();
            };

            if (didSolve)
            {
                alert(didSolve);
                //TRUE
            }
            else
            {
                //FALSE
            }

Screenshot true but false

UPDATE: Added $get function as this might be causing my problem.

  • 1
    Because you use `Boolean` you are creating an object version of the `boolean` primitive. Objects are treated as truthy values. – Patrick Evans Jun 08 '16 at 20:11
  • It's doing it in my environment aswell. When I change `if (didSolve)` to `if (didSolve == true)` it seems to work however. – GrumpyCrouton Jun 08 '16 at 20:18
  • 1
    In a previous version of the code you had `var didSolve = new Boolean(0);`, and it was explained to you why that wouldn't work. Now you have `var didSolve = false;` - are you saying you have tested the code again after making that change and you're still getting the same results? – tex Jun 08 '16 at 20:33
  • @Tex yes but I updated that to match what I meant, I think it doesn't update the variable inside the get function. I fixed it though. I just moved the code inside the get function and then it does work :D – Menno van Leeuwen Jun 08 '16 at 20:35
  • 1
    Yes, you have to move the code inside the `$get()` function because that's an asynchronous function. The way you had originally written it, all of the code after the `$get()` would have been executed before the `$get()` call finished. Also, the way the code is now displayed is misleading, since I can guarantee that the alert won't say 'false' after you changed the code to use `var didSolve = false;`. – tex Jun 08 '16 at 20:38

2 Answers2

1
new Boolean(0) == false; //equals to true
new Boolean(0) === false; //equals to false

new Boolean(0) is an object, not a simple boolean. i guess JS simply sucks that much that it doesn't cast it to a true "false".

Have you tried using

didSolve = false;

instead?

edit: see also: Why does !new Boolean(false) equals false in JavaScript?

Community
  • 1
  • 1
Franz Gleichmann
  • 3,420
  • 4
  • 20
  • 30
1

Lint your code - you should not be using Boolean as a constructor.

var thing = new Boolean(0) // won't work like you expect.

var thing = Boolean(0) // will work like you expect.

jsLint will warn you if you use Boolean as a constructor.

MDN clearly says, "do not use a Boolean object in place of a Boolean primitive."

tex
  • 2,756
  • 22
  • 31