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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2009
    Posts
    200
    Thanks
    23
    Thanked 0 Times in 0 Posts

    Two errors I get in JSLint for no good reason

    I checked the following code in JSLint:

    Code:
    <!DOCTYPE html>
    <html>
    
    <head>
        <meta charset="UTF-8">
        <title>Reset textarea</title>
    </head>
    
    <body>
        <textarea id="ta">
            <p>Hello</p>
        </textarea>
        <input type="button" value="Reset" onclick="reset();">
        <script>
            function reset() {
                var ta = document.getElementById('ta');
                if (!ta.value || ta.value != ta.defaultValue && confirm('Are you sure?')) {
                    ta.value = ta.defaultValue;
                }
            }
        </script>
    </body>
    
    </html>
    DEMO

    And I got the following errors:

    Expected '!==' and instead saw '!='.
    Since the textarea value and default value are always of the type string, then there's no type conversion before comparison and it makes absolutely no difference except making your JS file larger!

    The '&&' subexpression should be wrapped in parens.
    Why should I add parenthesis when && has a higher precedence than ||? I can simply remove nested parenthesis instead of:

    Code:
    if (!ta.value || (ta.value != ta.defaultValue && confirm('Are you sure?')))
    Please correct me if I'm mistaken.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    JSLint is often more pedantic and nit-picking than necessary. Your code is fine for any practical purpose.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    Rain Lover (03-31-2014)

  • #3
    Regular Coder
    Join Date
    Nov 2009
    Posts
    200
    Thanks
    23
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    JSLint is often more pedantic and nit-picking than necessary. Your code is fine for any practical purpose.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Thanks for the confirmation!

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You should NEVER use == or != in JavaScript unless you specify one of the type conversion functions on one side. Regardless of what the types on either side of the comparison are now you only need a small typo in your next update to have a field with the wrong type and have a loose comparison break.

    All of the tests that jsLint perform are there because someone ended up spending many hours debugging a problem caused by using one of the constructs it warns against using.

    Anyway if your values are the same type then adding the extra = will not change the way the code runs while making it less error prone so you'd be stupid not to add it in.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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