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);
Related
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)
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.
This question already has answers here:
Why the result of bool(true) && string is string in javascript?
(4 answers)
Closed 8 years ago.
I was rambling with JavaScript code in my application today when I observed something strange.
var someVar = 25;
var anotherVar = 50;
var out = (anotherVar == 50 && someVar);
console.log(out) // outputs 25 and not true or false;
Any idea what's happening?
As stated on MDN's Logical Operators page, the && operator:
Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.
In your case, expr1 (anotherVar == 50) is true (not false), so it returns expr2 (someVar), which is 25.
It doesn't return true or false because expr2 isn't a Boolean value.
The ECMA-262 Specification notes:
The value produced by a && or || operator is not necessarily of type Boolean. The value produced will always be the value of one of the two operand expressions.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Implied string comparison, 0='', but 1='1'
Executing the following case in javascript, I get 0 equal to '' (an empty string)
var a = 0;
var b = '';//empty string
if(a==b){
console.log('equal');//this is printed in console
}else{
console.log('not equal');
}
Could anyone guide that what is the reason behind this?
Quoting the doc (MDN):
Equal (==)
If the two operands are not of the same type, JavaScript converts the
operands then applies strict comparison. If either operand is a number
or a boolean, the operands are converted to numbers if possible; else
if either operand is a string, the other operand is converted to a
string if possible.
As a operand type here is Number, b gets converted to Number as well. And Number('') evaluates to 0.
This can be quite surprising sometimes. Consider this, for example:
console.log(0 == '0'); // true
console.log(0 == ''); // true
console.log('' == '0'); // O'RLY?
... or this:
console.log(false == undefined); // false
console.log(false == null); // false
console.log(null == undefined); // fal.... NO WAIT!
...and that's exactly why it's almost always recommended to use === (strict equality) operator instead.
0, "" (Empty String), false all technically have the same value, when typecasted. If you need to strictly treat them, you can use ===.
It is the same with similar programming languages, like PHP.
var a = 0;
var b = ''; //empty string
if(a == b){
console.log('equal'); //this is printed in console
}else{
console.log('not equal');
}
To make a strict comparison:
if(a === b){
console.log('equal');
}else{
console.log('not equal'); //this is printed in console
}
== operator in javascript don't compare types so 0=='' === true (because as a number string '' evaluates to 0) or 0==false === true (because bool false evaluates to 0) to compare types to you can use === operator.
Here you'll find useful information about comparison.
Javascript automatically converts variables of different types for comparison. That's a common feature in non-strictly typed languages.
If you need to compare variables and check the type, use the === operator.
In most programming language(including javascript) ""(string), 0(integer), \x0(null) losely mean the same thing: "empty". What's happening is your javascript engine finds "" == 0 false, due to the == it converts 0 to an integer. Again this is false, so it converts 0 to null which is false, so then it converts 0 to an empty string. (Not sure if this is the correct order of conversion). To make the condition "exact" match(no conversion) use === inplace of ==
== does typecasting. Always use ===.
In your example, the empty string of b is being converted to 0. So both a and b are the same.
Because empty string represented as number is zero. If you compare apples and oranges you should think of how your particular orange would look if it were an apple.
Because of coercion. It is usually a better idea to use === for comparisons in JavaScript.
Because == checks value equality so false, 0 and empty strings are equals. Use identity check ===.
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