By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,822 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,822 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Feb 10 '07 #1
Share this Question
Share on Google+
5 Replies

P: n/a
On Feb 10, 4:05 pm, "Ray" <ray_use...@yahoo.comwrote:
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

Feb 10 '07 #2

P: n/a
On Feb 10, 12:05 am, "Ray" <ray_use...@yahoo.comwrote:
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.
>
Thanks!
Ray

Feb 10 '07 #3

P: n/a
Ray
Many thanks, Rob, Benjamin! I learned something new :)

Feb 12 '07 #4

P: n/a
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

Feb 12 '07 #5

P: n/a
On Feb 10, 10:06 am, "Benjamin" <musiccomposit...@gmail.comwrote:
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

Feb 12 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.