Results 1 to 2 of 2

Thread: Two errors I get in JSLint for no good reason

  1. #1
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default 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. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by Rain Lover View Post
    there's no type conversion before comparison and it makes absolutely no difference except making your JS file larger!
    One extra equals sign is not a significant difference in file size. Are you minifying your scripts and html markup? That would be a much larger bandwidth savings.

    Generally speaking, triple equals (or bang-double equals) should always be used, unless you specifically want type conversion. JSLint marks this as an error because it is a very common source of hard-to-find mistakes (many coders simply don't understand the difference between == and === in the first place; and even experienced programmers can easily overlook it).

    Quote Originally Posted by Rain Lover View Post
    Why should I add parenthesis when && has a higher precedence …?
    Because, as above, precedence is a major cause of unexpected behaviors. Even if you arranged it correctly, as desired, it's harder to read clearly and you risk errors cropping up if you ever have to modify the code.

    So, to answer your question: no, you're not mistaken, and your code is not really "wrong."

    But yes, JSLint did point those issues out for a good reason. JSLint looks for best practices, not technical correctness. It's reminding you to make your code obviously correct.

  3. The Following User Says Thank You to traq For This Useful Post:

    Rain Lover (04-01-2014)

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2013, 12:25 AM
  2. IE problem - appendChild or other reason?
    By jonas-e in forum JavaScript
    Replies: 0
    Last Post: 08-03-2009, 06:40 PM
  3. preference or reason
    By bluewalrus in forum CSS
    Replies: 5
    Last Post: 12-29-2008, 09:18 AM
  4. what is the reason?
    By sweetboy in forum HTML
    Replies: 3
    Last Post: 05-03-2007, 10:54 AM
  5. for some reason www is needed
    By benslayton in forum Other
    Replies: 1
    Last Post: 09-10-2006, 10:00 PM

Bookmarks

Posting Permissions

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