HighDots Forums  

re: jslint suggests using '==='`

Javascript JavaScript language (comp.lang.javascript)


Discuss re: jslint suggests using '==='` in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
James Black
 
Posts: n/a

Default re: jslint suggests using '==='` - 03-01-2006 , 09:18 PM






Is '===' a valid operator in javascript?

Here is the output from jslint:
Problem at line 155 character 48: Use '===' to compare with '0'.

if(Annotation._rowelem.childNodes.length == 0) {




Thank you for helping me with the syntax.


Reply With Quote
  #2  
Old   
Hal Rosser
 
Posts: n/a

Default Re: jslint suggests using '==='` - 03-01-2006 , 09:32 PM







"James Black" <planiturthian (AT) gmail (DOT) com> wrote

Quote:
Is '===' a valid operator in javascript?

Here is the output from jslint:
Problem at line 155 character 48: Use '===' to compare with '0'.

if(Annotation._rowelem.childNodes.length == 0) {

According to
http://www.w3schools.com/js/js_operators.asp
It is a valid comparison operator.
the === operator returns true if both operands have the same value and are
the same type.
(5 === "5") would return false according to those rules
Is that 'strict equality' ?
HTH




Reply With Quote
  #3  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default re: jslint suggests using '==='` - 03-01-2006 , 09:56 PM



James Black wrote:

Quote:
Is '===' a valid operator in javascript?
It is. See <http://pointedears.de/scripts/js-version-info> for details.

Quote:
Here is the output from jslint:
Problem at line 155 character 48: Use '===' to compare with '0'.
jslint often provides good hints, but not always. Its recommendations
must apply to a general programming context; because it does not include
A.I., it cannot recognize the actual context an expression is used in.

Quote:
if(Annotation._rowelem.childNodes.length == 0) {
There is no need for a strict equality comparison `===' here; both operands
are supposed to be values of type `number'. That said, IMHO there is
seldom a need for using this operator at all. It is not fully backwards
compatible, and the gain in efficiency over using `==' is usually
negligible.

What jslint apparently is not telling you:

You are probably referring to Annotation._rowelem.childNodes in the
following block. If so, you should consider assigning the reference to
a local variable, and using the variable instead. This greatly improved
efficiency of your code.


HTH

PointedEars


Reply With Quote
  #4  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: jslint suggests using '==='` - 03-01-2006 , 10:04 PM



Hal Rosser wrote:

Quote:
"James Black" [...] wrote [...]:
Is '===' a valid operator in javascript?

Here is the output from jslint:
Problem at line 155 character 48: Use '===' to compare with '0'.

if(Annotation._rowelem.childNodes.length == 0) {

According to
http://www.w3schools.com/js/js_operators.asp
It is a valid comparison operator.
One of the few occasions where w3schools.com is correct. Stop referring to
it, refer to official language reference material instead as pointed out in
the newsgroup's FAQ, and in the en.Wikipedia articles about JavaScript,
JScript, and ECMAScript.

Quote:
the === operator returns true if both operands have the same value and are
the same type.
True.

Quote:
(5 === "5") would return false according to those rules
Correct.

Quote:
Is that 'strict equality' ?
It is. `==' involves implicit type conversion of operands if required,
`===' does not.


PointedEars


Reply With Quote
  #5  
Old   
Douglas Crockford
 
Posts: n/a

Default Re: jslint suggests using '==='` - 03-02-2006 , 12:19 AM



James Black wrote:
Quote:
Is '===' a valid operator in javascript?

Here is the output from jslint:
Problem at line 155 character 48: Use '===' to compare with '0'.

if(Annotation._rowelem.childNodes.length == 0) {
Your statement appears to mean

if (Annotation._rowelem.childNodes.length === 0) {

but actually means

if (!Annotation._rowelem.childNodes.length) {

You should use the form that means what you mean.

Constructs that appear to mean one thing but mean another can lead to
misinterpretations, misunderstandings, and errors. In a language as loose as
JavaScript, rigor in expression can help improve the resilience and portability
of our programs.

http://www.JSLint.com


Reply With Quote
  #6  
Old   
Hal Rosser
 
Posts: n/a

Default Re: jslint suggests using '==='` - 03-02-2006 , 01:34 AM




"Thomas 'PointedEars' Lahn" <PointedEars (AT) web (DOT) de> wrote

Quote:
Hal Rosser wrote:

"James Black" [...] wrote [...]:
Is '===' a valid operator in javascript?

Here is the output from jslint:
Problem at line 155 character 48: Use '===' to compare with '0'.

if(Annotation._rowelem.childNodes.length == 0) {

According to
http://www.w3schools.com/js/js_operators.asp
It is a valid comparison operator.

One of the few occasions where w3schools.com is correct. Stop referring
to
it,
**************
Ah, but the FAQ references W3Schools.com
http://www.jibbering.com/faq/#FAQ3_2

****************


refer to official language reference material instead as pointed out in
Quote:
the newsgroup's FAQ, and in the en.Wikipedia articles about JavaScript,
JScript, and ECMAScript.

the === operator returns true if both operands have the same value and
are
the same type.

True.

(5 === "5") would return false according to those rules

Correct.

Is that 'strict equality' ?

It is. `==' involves implicit type conversion of operands if required,
`===' does not.


PointedEars



Reply With Quote
  #7  
Old   
Randy Webb
 
Posts: n/a

Default Re: jslint suggests using '==='` - 03-02-2006 , 01:37 AM



Thomas 'PointedEars' Lahn said the following on 3/1/2006 9:56 PM:
Quote:
James Black wrote:

Is '===' a valid operator in javascript?

It is. See <http://pointedears.de/scripts/js-version-info> for details.
Don't see it. And to explain my statement fully, let me quote something
to you Thomas:

Stop referring to [non-official sites], refer to official language
reference material instead as pointed out in the newsgroup's FAQ, and in
the en.Wikipedia articles about JavaScript, JScript, and ECMAScript.

Practice what you preach!

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/



Reply With Quote
  #8  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: jslint suggests using '==='` - 03-02-2006 , 02:18 PM



Douglas Crockford wrote:

Quote:
[...] In a language as loose as JavaScript, rigor in expression
can help improve the resilience and portability of our programs.
And it can hinder, if not prevent that, too. For example, as
I said, the `===' operator is not fully backwards compatible.


PointedEars


Reply With Quote
  #9  
Old   
Luke Matuszewski
 
Posts: n/a

Default Re: jslint suggests using '==='` - 03-02-2006 , 05:28 PM




Thomas 'PointedEars' Lahn napisal(a):
Quote:
For example, as
I said, the `===' operator is not fully backwards compatible.
This always depends on requirements of the software you write, which
every good project has (and well paid of).
The operators like === or !== or 'delete' (since NN4, IE4 - ~October
1997) are very often used to provide better flexibility on
understanding program code. The same are anonymous and named function
expressions (very usefull). If you persist on not using ===, you must
use some sort of emulation of it, like:

function strictEquality(opA, opB)
{
if(typeof(opA)!='undefined' && typeof(opB)!='undefined' ) {
if(opA==null && opB==null) {
return true;
} else if(opA!=null && opB==null) {
return false;
} else if(opA==null && opB!=null) {
return false;
} else {
if(typeof(opA) == typeof(opB)) {
return opA == opB;
} else {
return false;
}
}

} else if(typeof(opA)!='undefined' && typeof(opB)=='undefined' ) {
return false;
} else if(typeof(opA)=='undefined' && typeof(opB)!='undefined' ) {
return false;
} else if(typeof(opA)=='undefined' && typeof(opB)=='undefined' ) {
return true;
}
}

, which is completely awkard.

For me the JavaScript language must support === !== and 'delete'
operators plus anonymous/named function expressions, object/array
initializer <- these features where supported since IE4/NN4 (~October
1997) so it is safe to use them.

Best regards.
Luke Matuszewski.



Reply With Quote
  #10  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: jslint suggests using '==='` - 03-02-2006 , 06:21 PM



Luke Matuszewski wrote:

Quote:
Thomas 'PointedEars' Lahn napisal(a):
For example, as
I said, the `===' operator is not fully backwards compatible.

This always depends on requirements of the software you write, which
every good project has (and well paid of).
The operators like === or !== or 'delete' (since NN4, IE4 - ~October
1997)
I have read of user agents for newer devices that support only features
once only found in older Web browsers for PCs.

Quote:
are very often used to provide better flexibility on understanding program
code.
Real understanding of program code includes understanding of the underlying
language concepts, such as loose typing and implicit type conversion.
Sometimes this feature is even _wanted_.

Quote:
The same are anonymous and named function expressions (very usefull).
True.

Quote:
If you persist on not using ===, you must use some sort of emulation of
it, [...]
Your logic is flawed.


PointedEars


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.