How true+false===1 evaluates to true [duplicate] - javascript

This question already has answers here:
Is true == 1 and false == 0 in JavaScript?
(10 answers)
What exactly is Type Coercion in Javascript?
(9 answers)
Closed 3 years ago.
I am trying to compare true+false to 1 i.e true+false===1
In the past, I tried the following:
true===1 //false
false===0 //false
true-true===0 //true
I expect the output to be false but the actual output is true.

true and false are implicitly type casted to number when used in mathematical operation
Note:- here + is type casting it to number
console.log(+true)
console.log(+false)
console.log(true+false)
so
true + false === 1
is actually
1 + 0 === 1

When you add up two Booleans, since the Booleans do not support arithmetic operation, but logic operation only, the JS environment just converts them to Number. So 1+0===1. But if you do true ===1, this is type sensitive, it will result false. If you do true == 1, it’ll be true. Cuz this is not type sensitive, type transformation will be done by JS environment.

Related

Why isn't 2+3 == "2+3"? [duplicate]

This question already has answers here:
Add string to a number in Javascript
(2 answers)
Closed 2 years ago.
Consider the following:
var comp = 2+3;
console.log(comp == "5"); // true
console.log(comp === "5"); // false
console.log(comp == "2+3"); // false
Why isn't the 3rd comparison true? JS can convert a string to integer only if it's a single number? What happens in the 3rd comparison? Is it unable to convert "2+3" into a number so it just assumes it's different?
comp, which is 5 is a number type in JS and "5" is a string. A string and a number are not the same and thus console.log(comp === "5") will return false.
When you use == instead of === it returns true, because of a concept called "type coercion". This just means that JS assumes that you are trying to compare two of the same types—in this case numbers—because they seem to be identical, and returns true. So it converted the string to a number to help you out.
In the third case there are other types of characters in the string, in addition to numbers, and none of them are similar to the number 5, so type coercion does not apply and so it returns false.
Read more about type coercion here.
Because "2+3" with quotation marks signals javascript that this is an string and it cant be coerced to an number.
And 5 is not the same like "2+3".
Just test it: console.log(typeof '2+3') and console.log(typeof 2 + 3)

Can I get explanation for (1 + undefined) implicitly Coercion weird behavior in JS? [duplicate]

This question already has answers here:
Why is `null + 1 = 1` but `undefined + 1 = NaN`?
(3 answers)
Closed 3 years ago.
I understand the implicit coercion that happens in javascript, but i actually can't understand why this happens:
isNaN(1 + null) // false
isNaN(1 + undefined) // true
As i know that JS convert the null & undefined to 0, Or we could say consider them false value.
what is the difference here ?
Javascript converts null to 0 but undefined is converted to NaN.
check it out using the built in fucntion Number .
So in this case (1+ null) is similar to 1+ 0 which is 1
and (1+ undefined) is similar 1 + NaN which is NaN.
That's why it returns true.
console.log(Number(null));
console.log(Number(undefined));
The weird behavior here though is that Javascript consider both of these types "false" when dealing with comparison operator or as a condition in if statement and false value considered 0 after implicitly coerced...
Conclusion: Js coerce undefined & null depending on the situation. if it's in condition, it try to convert it to boolean false & if it's in an arithmetic operation it try to convert it to numbers, in this case there would be difference between them:
undefined ==> NaN
null ==> 0

blank string is true or false in Javascript? [duplicate]

This question already has answers here:
In javascript, is an empty string always false as a boolean?
(6 answers)
Closed 4 years ago.
'' == false // true
' ' == false // true
if ('') {
// this code is not run
}
if (' ') {
// this code is run
}
As you can see, there are interesting results.
The empty string is treated as falsy, as we already know.
However, whitespace strings are treated as truthy or falsy, depending on the situation.
You can see the link posted in your question about toBoolean behavior. Another interesting tidbit;javascript has another comparison operator === that checks for value and type
'' == false // true
'' === false // false
A blank String is false.
console.log(' '==true);
console.log(''==true);
This is often the problem with the == operator; it attempts to coerce the types of the objects it is comparing to be equal. If you use the === operator, it will not attempt type coercion.
console.log(''===false);

variable set to minus number evaluates to true [duplicate]

This question already has answers here:
All falsey values in JavaScript
(4 answers)
Closed 6 years ago.
This javascript console log is confusing me.
How can x which has the value of -1 be evaluated to true in the if statement where it false the line before it? Thanks
x = -1
-1
x == true
false
if(x){console.log('yes')}
yes
When you test x in your if statement, you're not checking if x is true, you're checking if x is truthey. The rules for truthiness in javascript are:
empty strings are falsey. all other strings are truthy.
0 and NaN are falsey. all other numbers are truthy.
all objects and arrays are truthy.
null and undefined are falsey.
if(-1){console.log('yes')}
prints 'yes' in console because -1 and other numbers except 0 are truthey value, despite -1 not equal to true.
Here is a good source for extending knowledge related to javascript truthey and falsey values.

Difference between == and === in JavaScript [duplicate]

This question already has answers here:
Which equals operator (== vs ===) should be used in JavaScript comparisons?
(48 answers)
Closed 3 years ago.
What is the difference between == and === in JavaScript? I have also seen != and !== operators. Are there more such operators?
Take a look here: http://longgoldenears.blogspot.com/2007/09/triple-equals-in-javascript.html
The 3 equal signs mean "equality without type coercion". Using the triple equals, the values must be equal in type as well.
0 == false // true
0 === false // false, because they are of a different type
1 == "1" // true, automatic type conversion for value only
1 === "1" // false, because they are of a different type
null == undefined // true
null === undefined // false
'0' == false // true
'0' === false // false
=== and !== are strict comparison operators:
JavaScript has both strict and
type-converting equality comparison.
For strict equality the objects being
compared must have the same type and:
Two strings are strictly equal when they have the same sequence of
characters, same length, and same
characters in corresponding positions.
Two numbers are strictly equal when they are numerically equal (have
the same number value). NaN is not
equal to anything, including NaN.
Positive and negative zeros are equal
to one another.
Two Boolean operands are strictly equal if both are true or
both are false.
Two objects are strictly equal if they refer to the same Object.
Null and Undefined types are == (but not ===). [I.e. (Null==Undefined) is true but (Null===Undefined) is false]
Comparison Operators - MDC

Categories

Resources