Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If (value === true) not working

Tags:

javascript

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");
like image 759
Igor-Vuk Avatar asked Dec 24 '16 18:12

Igor-Vuk


People also ask

Is false === 0 in JS?

In JavaScript “0” is equal to false because “0” is of type string but when it tested for equality the automatic type conversion of JavaScript comes into effect and converts the “0” to its numeric value which is 0 and as we know 0 represents false value. So, “0” equals to false.

Why we use === in JavaScript?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

How check boolean value in if conditions in TypeScript?

Use the typeof operator to check if a value is of boolean type, e.g. if (typeof variable === 'boolean') . The typeof operator returns a string that indicates the type of a value. If the value is a boolean, the string "boolean" is returned. Copied!

How do I check if JavaScript is genuine?

Use the strict equality (===) operator to check if a variable is equal to true - myVar === true . The strict equality operator will return true if the variable is equal to true , otherwise it will return false . Copied!


1 Answers

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");
like image 154
Scott Marcus Avatar answered Oct 12 '22 21:10

Scott Marcus