6

Can someone please explain to me why this is not working? The value is true, it is boolean and if I check it like I usually do if(value) {}; it works. Why not like this?

function updateRecords(value) {

  console.log(!!(value));        // true
  console.log(typeof(!!(value)));    //boolean

    if (value === true) {
        alert("success");
      }
 }

    updateRecords("Take a Chance on Me");
Igor-Vuk
  • 3,551
  • 7
  • 30
  • 65
  • 3
    It is not a boolean. The "thing" you were running `typeof` against *was* a boolean, due to type casting. To prove it, try logging "typeof(value)" – wally Dec 24 '16 at 18:32

3 Answers3

13

The value that you are working with is a string, not a Boolean. When you wrote this:

console.log(!!(value));

You were converting the string value to a Boolean, but you weren't capturing it. You converted it and logged it and then it got thrown away. Then your next line:

console.log(typeof(value));  // string NOT Boolean

went back to testing the original value of value (a string).

The triple equal sign checks for "type and value equality", so your if test fails.

Now, if you remove one of the equal signs (==) and test for simple "value equality with type conversion", it still won't work unless the text you are testing against converts to the same number that true will (see link below for details), but that won't happen, your string will convert to NaN (not a number) and true will convert to 1, so even value == true will fail.

You can see more details about how equality and type conversion work together here.

At any rate, in this case don't test against the Boolean true, just test for the existence of data: if(value) which doesn't attempt to convert your value to a number, it attempts to convert it to a Boolean. As long as you don't have a string that contains "falsy" values (ie. "", "0", "false", " "), it will convert to true

function updateRecords(value) {

  console.log(!!value);        // true
  console.log(typeof value);    // string

    // Don't test against true (that's implied), just test the data.
    if (value) {
        alert("success");
    }
 }

updateRecords("Take a Chance on Me");

Or, capture the casted version of your data and then you can use ===

function updateRecords(value) {
  // Convert the value of "value" to a Boolean and
  // store that value back in the original variable
  value = !!value;

  // Now, let's test value AFTER capturing its converted value
  console.log(value);            // true
  console.log(typeof value);    // boolean

    if (value === true) {
        alert("success");
      }
 }

updateRecords("Take a Chance on Me");
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
2

Because you are not yet typecasting in the second instance.

if (!!(value) === true) {
    alert("success");
}

Previously you were checking:

"Take a Chance on Me" === true      // This obviously returns false.

The above should work. Else you shouldn't use the === operator. You should use ==:

if (value == true) {
    alert("success");
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

JS can be surprising (@praveenkumar alias flashgordon : not always obvious) :

""   == false is true
"1"  == false is false (ok so far)
"0"  == false is true  (looks weird)
" "  == false is true  (wtf...)
"\n" == false is true  (wtf²)