Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Location
    England
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    as the message says
    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.

    Will someone please explain what I have to change to get rid of the validation error.
    ask Lint:
    (use === or !==)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Location
    England
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,391
    Thanks
    11
    Thanked 568 Times in 561 Posts
    Lint tells you exactly what to do. You just didn't understand that you need to replace == with ===

  • Users who have thanked devnull69 for this post:

    SpanishBoy (08-20-2012)

  • #5
    New to the CF scene
    Join Date
    Aug 2012
    Location
    England
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    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:
    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.
    Last edited by Dormilich; 08-20-2012 at 02:26 PM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    SpanishBoy (08-20-2012)

  • #7
    New to the CF scene
    Join Date
    Aug 2012
    Location
    England
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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!

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,215
    Thanks
    12
    Thanked 338 Times in 334 Posts
    have you ever heard of the term "operator precedence" ?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •