473,388 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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
5 8770
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
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
Ray
Many thanks, Rob, Benjamin! I learned something new :)

Feb 12 '07 #4
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
5
by: Curtis Gilchrist | last post by:
I am required to read in records from a file and store them in descending order by an customer number, which is a c-style string of length 5. I am storing these records in a linked list. My...
6
by: sridhar | last post by:
#include <stdio.h> int main(){ unsigned int ui = 0; if(0x0ul <= ui){ printf("less eq\n"); } } On my system unsigned long is 64 bits and unsigned int is 32.The compiler gives a warning
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
1
by: Will Chamberlain | last post by:
I just created an application that displays data from 2 sources in a datagrid. There are 2 repeaters nested in the datagrid (probably not the best idea). The whole purpose of this application is to...
4
by: Frank | last post by:
Hello, Developing an app where the user fills out a sometimes quite lengthy form of chkboxes, txtboxes, radbtns, etc. User responses are saved to a mySql db, which the user can later edit. When...
20
by: Bill Pursell | last post by:
This question involves code relying on mmap, and thus is not maximally portable. Undoubtedly, many will complain that my question is not topical... I have two pointers, the first of which is...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
1
by: Patrick C | last post by:
hey everyone, i'm going to be comparing data in a list. Just basic >= or <= type stuff. However, the list i have somtimes starts with 'n/a'. That is somtimes it starts like this: data = or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.