Rain Lover
03-31-2014, 03:32 PM
I checked the following code in JSLint (http://www.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 (http://jsfiddle.net/Mori/e2Fzm/)
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 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table) than ||? I can simply remove nested parenthesis (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Removing_nested_parenthesis) instead of:
if (!ta.value || (ta.value != ta.defaultValue && confirm('Are you sure?')))
Please correct me if I'm mistaken.
<!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 (http://jsfiddle.net/Mori/e2Fzm/)
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 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table) than ||? I can simply remove nested parenthesis (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Removing_nested_parenthesis) instead of:
if (!ta.value || (ta.value != ta.defaultValue && confirm('Are you sure?')))
Please correct me if I'm mistaken.