browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need JavaScript / Ajax / DHTML help?

Get answers from our community of JavaScript / Ajax / DHTML experts on BYTES! It's free.

Why is comparing with just "!=" not good enough?

Ray
Guest
 
Posts: n/a
#1: Feb 10 '07
Hello,

I've been running my scripts through Douglas Crockford's JSLint. I
notice that it keeps complaining when I do this:

if (da != null) {

it says "Use !== to compare with null".

It does the same for comparison with undefined.

It also complains when I check whether soemthign is true or not:

Use '===' to compare with 'true'.

My question is, what is the reason behind it? Won't != and == work
equally well in the cases above?

Thanks!
Ray




RobG
Guest
 
Posts: n/a
#2: Feb 10 '07

re: Why is comparing with just "!=" not good enough?


On Feb 10, 4:05 pm, "Ray" <ray_use...@yahoo.comwrote:
Quote:
Hello,
>
I've been running my scripts through Douglas Crockford's JSLint. I
notice that it keeps complaining when I do this:
>
if (da != null) {
>
it says "Use !== to compare with null".
>
It does the same for comparison with undefined.
>
It also complains when I check whether soemthign is true or not:
>
Use '===' to compare with 'true'.
>
My question is, what is the reason behind it? Won't != and == work
equally well in the cases above?
Because it wants you to think about why you're comparing to null:

var x = undefined;
alert( x == null ) // shows true
alert( x === null ) // shows false

It depends on whether you want x to be equivalent to null or *exactly*
null. It's called the strict equality operator for a reason! :-)


--
Rob

Benjamin
Guest
 
Posts: n/a
#3: Feb 10 '07

re: Why is comparing with just "!=" not good enough?


On Feb 10, 12:05 am, "Ray" <ray_use...@yahoo.comwrote:
Quote:
Hello,
>
I've been running my scripts through Douglas Crockford's JSLint. I
notice that it keeps complaining when I do this:
>
if (da != null) {
>
it says "Use !== to compare with null".
>
It does the same for comparison with undefined.
>
It also complains when I check whether soemthign is true or not:
>
Use '===' to compare with 'true'.
>
My question is, what is the reason behind it? Won't != and == work
equally well in the cases above?
Using !== is the only way to insure the if what you have is null. For
example:

var x = getNumberOrNull();
if (x != null) {

}

In this example, getNumberOrNull() could return zero. Zero and null
both evaluate to false so the if block would be skipped if x was zero
(false eqauls false). However, !== tests for type and boolean value so
only a true null value would cause the expression to return false.
Quote:
>
Thanks!
Ray

Ray
Guest
 
Posts: n/a
#4: Feb 12 '07

re: Why is comparing with just "!=" not good enough?


Many thanks, Rob, Benjamin! I learned something new :)

Isaac Schlueter
Guest
 
Posts: n/a
#5: Feb 12 '07

re: Why is comparing with just "!=" not good enough?


If you just want to know if "x" is truish, use
if (x) {

If you want to know if it's falsey, use
if (!x) {

(x == null) only tests if x is falsey, since (false == 0) and ('' ==
false) and (undefined == null). If you're going to bother writing out
(x == true), then it must matter that x is boolean "true" and not 1 or
"asdf" or [1,2,3], so you should use ===. If it doesn't matter, then
skip the "== true" to make it clearer.

undefined and null are oddballs, since they're falsey, but != false.
(null == undefined) but (null !== undefined).

--
Isaac Z. Schlueter
http://isaacschlueter.com

Isaac Schlueter
Guest
 
Posts: n/a
#6: Feb 12 '07

re: Why is comparing with just "!=" not good enough?


On Feb 10, 10:06 am, "Benjamin" <musiccomposit...@gmail.comwrote:
Quote:
In this example, getNumberOrNull() could return zero. Zero and null
both evaluate to false so the if block would be skipped if x was zero
(false eqauls false).
That's not true in Javascript. undefined and null are their own
special class of things that don't get automatically typecast to
either true or false in equivalence tests.

alert( 0 != null ); // true
alert( 1 != null ); // true
alert( 0 == null ); // false
alert( undefined == null ); // true
alert( null == false ); // false
alert( null == true ); // false
alert( !null == true ); // true
alert( !0 == !null ); // true
alert( '' == 0 ); // true

--
Isaac Z. Schlueter
http://isaacschlueter.com

Closed Thread