Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the JSLint code quality tool

So I checked my javascript using this: http://www.jslint.com/

and if I don't wrap IF/FOR statements between { }, I get "errors" like this:

Problem at line 152 character 27: Expected '{' and instead saw 'reset()'.

or if I initialize variables inside FOR I get:

Problem at line 154 character 19: Move 'var' declarations to the top of the function.

Why are these considered errors? Shouldn't these be considered good practice instead because they actually reduce the size of the code, which for javascript is even more important than the speed of the code?

like image 644
Alexa Avatar asked Aug 24 '26 20:08

Alexa


1 Answers

For the if statement error, the lack of an opening brace is considered bad practice because it makes for harder maintenance. If, for example, you wanted to add another statement inside the if statement, you'd end up needing to add the braces anyway, so it makes maintenance easier just to include them in the first place.

The second error, I find a bit.. opinionated, since personally I prefer to declare variables closer to where they are used, instead of at the top of a function.

Personally, I prefer to use JSHint these days, since Crockfords Lint I find to be less about correct code, and more about Crockfords opinion of what JavaScript should look like, everyone else's opinion be damned.

like image 126
Karl Nicoll Avatar answered Aug 27 '26 11:08

Karl Nicoll



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!