Reputation: 73
many may find my mental block odd, but I fear that if I don't get this fixed now, it will cause me huge problems down the road.
From MDN on: http://api.jquery.com/Types/#String
var x = ""; // so x inits to false
if ( x ) {
console.log( "x defaulted to true" );
} else {
console.log( "x defaulted to false" );
}
// x defaulted to false
So, x being an empty string initializes x to value FALSE. Simple.
The code returns "x defaulted to false". i.e. the if condition resolves to false, so the second statement is returned.
I need some analogy to understand this. In my head I think: "If (Some Condition is true), i.e. "yes, it is correct (true) that X is indeed false" than return the appropriate reply.
As it is indeed true that x is FALSE, shouldn't this return true? Why not?
As a test I init x to false and check in the if statement to see if x is indeed false. However, in this case it returns TRUE, i.e., TRUE, X is indeed false.
var x = false;
if ( x === false ) {
console.log( "x defaulted to true" );
} else {
console.log( "x defaulted to false" );
}
// x defaulted to true
Why doesn't the first case do the same? In both cases the value of x is false. This keeps tripping me up.
Upvotes: 1
Views: 83
Reputation: 48
var x = ""; // so x inits to false
if ( x ) {
console.log( "x defaulted to true" );
} else {
console.log( "x defaulted to false" );
}
// x defaulted to false
When you say if with some condition expression it executes Boolean function on the condition expression i.e Boolean(condition expression)
In first case Boolean("") which is equal to false. In second case Boolean(x === false) results to true.
JavaScript operators will freely convert types. In the similar note if condition will convert condition expression to its boolean value.
Upvotes: 0
Reputation: 3541
Anyone here is able to tell you why you get that result, but I'll try and get a bit beyond that and guess that your "mental block" is probably caused by the implicit check that you do not see, but the code is actually doing. Initializing a variable to an empty string is not the same initializing it to false.
Your code
var x = "";
if ( x ) {
is actually checking that the variable contains something, because you initialized it to a string value. So, implicitly this means something like "if x contains something other than null or an empty string".
If you changed your code to
var x = ""; // so x inits to false
if ( x === "" ) {
console.log( "x defaulted to true" );
} else {
console.log( "x defaulted to false" );
}
then you'd get "x defaulted to true". Every time you do not have an explicit expression in the condition, but just a variable, there's an implicit check going on, basically depending on the variable type.
Upvotes: 0
Reputation: 4346
In the first case you are saying
if (x) ...
which is the same as saying "if x is truthy" ...
In your case, x
is equal to an empty string, which is falsey in javascript. As such, you are hitting the else
. Obviously in the second example, you are setting x = false
and then asking "Is x false?" - the answer being "yes, x is false" or true
which is why the first condition is executed.
The code that gets executed after the if/else is resolved is irrelevant to the truthiness / falseyness of the condition. You could console log "x defaulted to a banana" for all javascript (or any language) would care.
Edit - to add a bit more clarity, if you want the second code snippet you posted to hit the else
change it to if (x)
like your first snippet. Since false
is falsey - it will behave as you expect.
tl;dr
var x = false;
x === false // this is true
x // this is false
Upvotes: 1