0

Possible Duplicate:
Javascript === vs == : Does it matter which “equal” operator I use?

when i checked my code with JSLINT, it was full of errors...it works perfectly even in Ie6 but for some reason because i'm not an elegant coder i got more than 100 errors...

so i'd like to learn how to code better, error free... but when i used some suggestions JSLINT gave me like was to replace == with ===, well i did, and nothing was working...

so my question is: do we have to follow literally all the JSLINT suggestions?

Community
  • 1
  • 1
Francesco
  • 24,839
  • 29
  • 105
  • 152

3 Answers3

7

JSLint tests a professional subset of Javascript, created by Douglas Crockford. Essentially, he seeks to carve only the "good parts" out of the JavaScript language, to make life for programmers easier and code more legible and predictable. So, the short answer is NO. You do not have to follow the instructions. Perfectly valid JavaScript code will, and does, fail JSLint all the time.

But the long answer is that your life will be significantly easier in the long run if you do.

Take === vs. ==, for example. The difference is testing for EQUALITY versus testing for IDENTITY. Or, as Crockford says in his book, JavaScript: The Good Parts:

JavaScript has two sets of equality operators: === and !==, and their evil twins == and !=. The good ones work the way you would expect. If the two operands are the same type and have the same value, then === produces true and !== produces false. The evil twins do the right thing when the operands are of the same type, but if they are of different types, they attempt to coerce the values. The rules by which they do that are complicated and unmemorable.

So, if you're using === instead of ==, your code will almost always perform as you expect it to, a small price to pay for typing an extra character. But if you use == and the operands are not of the same type -- a string and an integer, for instance, JavaScript will first translate those values to the same type, and then compare them. And they might not do what you want or what you expected. And since, in JavaScript, you're often dealing with elements brought in from the DOM, which come in as strings, you run into string conversion issues all the time.

As Crockford writes, here are some of the issues you could run into:

'' == '0'   => false
0 == ''     => true
0 == '0'    => true

false == 'false'    => false
false == '0'       => true

Screwy, eh? JavaScript: The Good Parts is a great book, super thin, and if you've been working with JavaScript for a while, it will change the way you code for the better. So will JSLint, if you bow your head like a good marine.

So. Long answer? No. You don't have to do everything JSLint tells you. But you should.

Chris Ladd
  • 2,795
  • 1
  • 29
  • 22
  • Ahh! You're right, sorry! It's funny, I reference that book all the time, and in my head, I always say 'Crawford, crawford, crawford'... Maybe like [this](http://www.snopes.com/language/apocryph/cambridge.asp) – Chris Ladd Mar 29 '11 at 16:09
  • good answer!...so my question at this point is: which other code validator/tester do you guys use besides firebug? – Francesco Mar 29 '11 at 18:59
  • and btw, you sold it...i just order the book for kindle ehhehe – Francesco Mar 29 '11 at 19:01
  • Great! You'll love the book. Personally, I develop on a Mac, so I use the [developer tools that come installed with Safari](http://developer.apple.com/technologies/safari/developer-tools.html). Which are just awesome. – Chris Ladd Mar 29 '11 at 20:30
4

No. Doug Crockford will not personally show up at your house to beat you.

See Which equals operator (== vs ===) should be used in JavaScript comparisons? about == vs === though.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
2

You don't have to follow JSLint advices.

You don't have to write tests.

You don't have to avoid globals.

You don't have to check the code works before deploying.

You don't have to take candies from strangers.

But it can save your ass.

masylum
  • 22,091
  • 3
  • 20
  • 20
  • 4
    Taking candy from strangers can save my ass?? I've been doing it wrong for all these years. :p – camdez Feb 06 '12 at 19:32