Reputation: 955
I accidentally got undefined == true
and undefined == false
that both of them returns false.
But !undefined
returns true.
And this is the question:
What's the algorithmic difference(s) between !someVariable
and someVariable == false
?
If i want to explain it more, type undefined == false ? 't' : 'f'
in your browser's console, and it returns, 'f'
( as explained above ).
But if you type !undefined ? 't' : 'f'
it returns 't'
.
So obviously there's difference(s) between them and how they're working.
Update:
I'm asking what does javascript do after we write !someVar
or someVar == false
. I guess the second one convert both of them to a comparable value and compares them? What's about the first one?
Upvotes: 1
Views: 161
Reputation: 2193
undefined
is not a boolean value so when you use !
operator, your value will be converted to boolean at first.
but ==
operator just checking your values.
so if you want to get true from undefined == false
you should do it like Boolean(undefined) == false ? 't' : 'f'
or the short way like !!undefined == false
==
will convert both side to number like Number(undefined) == Number(false)
and because Number(undefined)
is NaN and Number(false)
is 0 the result is false.
just like 'true' == true
is false but Boolean('true') == true
is true
Upvotes: 2
Reputation:
'!' operator first simply invert the value of variable/result occuring in front of it and then check it.
But a==b
will compare the actual value of a
and b
.
For eg.:: Object p=null;
if(!(p==null))System.out.println("hi");
In above case p==null
will return true but !
will invert the result and check for trueness of that result i.e. if p
is not null then print "hi".
As we know ::
inverse of undefined is undefined
and undefined is not equal to any value among true or false
because true
is binary 1 and false
is binary 0 but undefined
is some garbage value and null
is also some garbage value.
So on comparing them using ==
operator result will always be false but !undefined
is undefined
itself so result is true
.
Hope, this will help you in understanding detailed difference b/w the operations of !
and ==
on undefined
or on any other variable .
Upvotes: 0
Reputation: 52932
From the ECMAScript specification:
11.9.1 The Equals Operator ( == )
The production EqualityExpression : EqualityExpression == RelationalExpression is evaluated as follows:
Let lref be the result of evaluating EqualityExpression. Let lval be GetValue(lref). Let rref be the result of evaluating RelationalExpression. Let rval be GetValue(rref). Return the result of performing abstract equality comparison rval == lval. (see 11.9.3).
.
11.4.9 Logical NOT Operator ( ! )
The production UnaryExpression : ! UnaryExpression is evaluated as follows:
Let expr be the result of evaluating UnaryExpression. Let oldValue be ToBoolean(GetValue(expr)). If oldValue is true, return false. Return true.
What you're looking at are two expressions:
!someVariable
will use ToBoolean()
and invert the result.
someVariable == false
will evaluate to true or false by an equality comparison.
Also keep in mind that ==
and ===
are not the same thing.
Upvotes: 2