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");