473,804 Members | 3,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8787
On Feb 10, 4:05 pm, "Ray" <ray_use...@yah oo.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...@yah oo.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.comwro te:
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
495
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 checking to see if the result has any 1s in it (if so, the arrays are different). This seems to be faster than comparing each bit of the two original arrays one at a time. But I still have to iterate over each element of the array, and I'd like to
5
2428
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 problem is in comparing the customer number I just read in with those in the list. I'm not sure how to go about comparing c-style strings for greater than, less than.. here is how I am currently trying to do it: while( ( custinfo.number >...
6
8072
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
22083
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 wondering if one is better than the other or if there is some sort of hybrid of the two that would be superior. IMPLEMENTATION 1: #ifndef HAVE_STRCASECMP
1
1320
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 visually display side-by-side data from these 2 sources so that the end-user can track changes. What I am wanting to do now is to highlight, bold, etc... rows where values are different. Layout (currently have): Column1(product) / Column2...
4
3722
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 the user chooses to edit, I pull the responses from the db, toss them in a dataset, check the checks, fill the txtboxes, etc.,etc. The user then adds, deletes, or changes entries as needed and clicks the Save Changes button. Here is where the fun...
20
2171
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 mmapped to a single page. I want to determine if the second is on the page. I'd like to do: #include "platform_appropriate_definition_of_PAGESIZE.h" int compare1(const char *a, const char *b)
2
3390
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 checkInteger(&$value, $checks) { $err = ''; if (!is_numeric($value) || (floatval($value) != intval($value))) { $err .= 'Input must be an integer. ';
25
13064
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 *p, void *q) that will take any two pointers and decide whether they can be compared or not. I really can't think how to do this - any suggestions?
1
1322
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 data = or
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10571
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.