Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-19-2012, 03:28 PM   PM User | #1
SpanishBoy
New to the CF scene

 
Join Date: Aug 2012
Location: England
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
SpanishBoy is an unknown quantity at this point
Lint warning: comparisons against null

I am getting the following error when I validate a page:

[JavaScript] lint warning: comparisons against null, 0, true, false, or an empty string allowing implicit type conversion (use === or !==)

The code it doesn't like is:
Code:
 if (place % 2 == 0) {
Will someone please explain what I have to change to get rid of the validation error.

I have done a search on the site and thread 187016 is similar, but it doesn't solve my problem.

Many thanks
SpanishBoy is offline   Reply With Quote
Old 08-20-2012, 07:54 AM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,865
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
as the message says
Quote:
comparisons against null, 0, true, false, or an empty string allowing implicit type conversion
a comparison using not the type-safe comparison allows values to correctly match the expression, even if they are not intended to. although it doesn’t play a role here since the result of a mathematical operation is always a number.

Quote:
Will someone please explain what I have to change to get rid of the validation error.
ask Lint:
Quote:
(use === or !==)
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 08-20-2012, 01:08 PM   PM User | #3
SpanishBoy
New to the CF scene

 
Join Date: Aug 2012
Location: England
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
SpanishBoy is an unknown quantity at this point
Thank you for your reply Dormilich.

I found "Lint" via Google and ran the code in their 'Online Lint'. This came up with exactly the same words as before! So asking Lint didn't tell me what to do and I still don't know how to correct the code.

I was hoping that someone could tell me how to correct the existing line by using "===" or "!==" instead of the existing code.
SpanishBoy is offline   Reply With Quote
Old 08-20-2012, 01:29 PM   PM User | #4
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Lint tells you exactly what to do. You just didn't understand that you need to replace == with ===
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
SpanishBoy (08-20-2012)
Old 08-20-2012, 02:12 PM   PM User | #5
SpanishBoy
New to the CF scene

 
Join Date: Aug 2012
Location: England
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
SpanishBoy is an unknown quantity at this point
Thank you devnull99.

I think that I now understand it

I tried it your way - changing to:
Code:
 if (place % 2 === 0) {
and it appears to be working OK.

I then found the page http://www.jslint.com/lint.html and this suggested:
Code:
 if (!place % 2) {
and that appears to work as well!

Neither one came up with an error on the 'Online Lint', so I presume both are correct.

Thank you once again.
SpanishBoy is offline   Reply With Quote
Old 08-20-2012, 02:16 PM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,865
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
nope. the second one won’t work correctly. for exactly the reason the warning was mentioning.
PHP Code:
var 1;
alert(!2); // 0 
bear in mind that Lint does look for certain string patterns, it does not run or analyse the JS code.

PS. Lint said something entirely different:
Quote:
If you only care that a value is truthy or falsy, then use the short form.
pay attention to the first part of the sentence. This advice is exclusively about expressions that do not require type-safety! And the author assumed that you can differentiate between an expression and a variable.
__________________
please post your code wrapped in [CODE] [/CODE] tags

Last edited by Dormilich; 08-20-2012 at 02:26 PM..
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
SpanishBoy (08-20-2012)
Old 08-20-2012, 02:28 PM   PM User | #7
SpanishBoy
New to the CF scene

 
Join Date: Aug 2012
Location: England
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
SpanishBoy is an unknown quantity at this point
OK thank you Dormilich.

I'll stick to the one that devnull69 suggested!

I guess I'm not cut out to be a great JavaScript programmer!
SpanishBoy is offline   Reply With Quote
Old 08-20-2012, 02:29 PM   PM User | #8
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,865
Thanks: 9
Thanked 291 Times in 287 Posts
Dormilich is on a distinguished road
have you ever heard of the term "operator precedence" ?
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

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

Forum Jump


All times are GMT +1. The time now is 08:34 AM.


Advertisement
Log in to turn off these ads.