Rules for Converting string and number to Boolean in JavaScript [duplicate] - javascript

This question already has answers here:
Equality of truthy and falsy values (JavaScript)
(3 answers)
All falsey values in JavaScript
(4 answers)
Understanding JavaScript Truthy and Falsy
(9 answers)
Closed 5 years ago.
I read this in Eloquent JavaScript book:
The rules for converting strings and numbers to Boolean values state
that 0, NaN, and the empty string ("") count as false, while all the
other values count as true.
Because of this, expressions like 0 == false and "" == false are true.
And also following these rules this expression should evaluate to true:
console.log("A" == true)
But it evaluates to false.
Why?

Related

How can an array of 1+ elements be at the same time not true and not false? [duplicate]

This question already has answers here:
What exactly is Type Coercion in Javascript?
(9 answers)
Coercion in JavaScript
(1 answer)
Why if([]) is validated while [] == false in javascript?
(3 answers)
If an array is truthy in JavaScript, why doesn't it equal true?
(4 answers)
Closed 3 years ago.
In Javascript, how can an element of 1+ elements return false if compared to a boolean?
> [1, 2] == true
< false
> [1, 2] == false
< false
I know that if the array contains only a 0 or a 1 a casting will be done and the comparison will return true if 0 is compared to false or if 1 is compared to true.

es6 meaning of double exclamation marks [duplicate]

This question already has answers here:
What is the !! (not not) operator in JavaScript?
(42 answers)
Closed 4 years ago.
in some es6 code I see following:
let layer_combined = layers.map(getLayer).filter(l => !!l);
what is the meaning of !! in the filter() return?
It's a way of casting to a boolean with double negation.
A singular ! negation produces a true/false value, but it's the opposite of what you want. Double negation produces a true/false value which matches the original intent.
Try it:
!!0 // false
!!1 // true
!!"test" // true
!!null // false

JavaScript Type Conversion Table [duplicate]

This question already has answers here:
How does JS type coercion work?
(2 answers)
Understanding JavaScript Truthy and Falsy
(9 answers)
Closed 5 years ago.
I have a question about type conversions. Given the following table from w3schools.com...
Why are the strings "0" and "000" converted to boolean true?
Because when you coerce a value to boolean in JavaScript, any non-blank string is true. Only blank strings are false.
The reason of it is because both "0" and "000" are strings and not numbers.
Any string which is non-empty converted to boolean is going to be true.

javascript: expecting true or false but getting number [duplicate]

This question already has answers here:
Why don't logical operators (&& and ||) always return a boolean result?
(9 answers)
Logical AND (&&) and OR (||) operators
(1 answer)
Closed 6 years ago.
I have something like the following:
var val = "string";
var testVal = val && val.length;
I would expect testVal to be either true or false but it is the length of the string. Not sure why this is?
The logical && operator doesn't perform a type casting. The expression it forms is just evaluated to the left-hand operand if this operand is falsy, otherwise to the right-hand operand.
You have to explicitly convert to boolean:
var testVal = !!(val && val.length);

In JavaScript, why is []==false true? [duplicate]

This question already has answers here:
Empty arrays seem to equal true and false at the same time
(10 answers)
Closed 7 years ago.
!![] is true while !!false is absolutely false. Why does []==false evaluate to true in JavaScript?
== does some type conversions before comparing. If you don't want to do the type conversion use === instead.
document.write([]==false); //true
document.write('<br>');
document.write([]===false); //false

Categories