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

R

Ray

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
 
R

RobG

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! :)
 
B

Benjamin

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.
 
I

Isaac Schlueter

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).
 
I

Isaac Schlueter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top