Forum Moderators: open

Message Too Old, No Replies

Two errors I get in JSLint for no good reason

         

Rain_Lover

3:09 pm on Mar 31, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



I checked the following code in JSLint [jslint.com]:


<!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 [jsfiddle.net]

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 [developer.mozilla.org] than ||? I can simply remove nested parenthesis [developer.mozilla.org] instead of:


if (!ta.value || (ta.value != ta.defaultValue && confirm('Are you sure?')))


Please correct me if I'm mistaken.

Fotiman

3:52 pm on Mar 31, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Since the textarea value and default value are always of the type string

The JavaScript has no way of knowing that "ta" is a textarea. If, for example, ta was an HTMLProgressElement (<progress id="ta"></progress>), then value could be a number type. The linter is more generic and is looking explicitly for cases of !=, which could be problematic in *some* cases.
Note, it's also configurable in JSLint whether or not to tolerate == and !=.


Why should I add parenthesis when && has a higher precedence than ||?

Did you mean this:
if (!ta.value || (ta.value != ta.defaultValue && confirm('Are you sure?')))
or this:
if (!ta.value || ta.value != (ta.defaultValue && confirm('Are you sure?')))

The point of the linter is to flag suspicious code. It doesn't necessarily indicate an error, but could bring attention to code that isn't doing what you think it's doing.

Rain_Lover

4:03 pm on Mar 31, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



Your explanation is perfect!

Did you mean this:
if (!ta.value || (ta.value != ta.defaultValue && confirm('Are you sure?')))
or this:
if (!ta.value || ta.value != (ta.defaultValue && confirm('Are you sure?')))


The first one.

Fotiman

4:05 pm on Mar 31, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




The first one.

Note, I was just trying to make that point that without the parenthesis it becomes less clear what the intent was, and the Linter is good at pointing out areas of code like that. :)

Rain_Lover

4:12 pm on Mar 31, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



Now I see.
Thanks very much! :)