Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I remember reading some "funny" examples once of weird results JavaScript can give when you, for example, add strings to numbers etc. Does anyone have any good examples or a link to the blog that I might have read them on? Which ones are likely to catch a programmer out in the real world?

share|improve this question

closed as too broad by MichaelT, durron597, GlenH7, Snowman, Dan Pichelman Jul 9 '15 at 19:14

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

Welcome to Programmers. This questions does not meet the guidelines to asking questions on this site. Please read the site's FAQ and edit it to meet more of those guidelines. This question would fit under mindless social fun and one that creates lists for answers, both of which are off-topic. – Walter Feb 1 '11 at 13:00

All the ones that are described in:

share|improve this answer

The biggest gotcha in JavaScript is that the "==" operation performs type type coercion and should almost always be avoided. Instead you should use the "===" to do a true boolean comparison.

For example, because of type coercion., 1 == true is true, but 1 === true is false. The == operator often hides type errors.

As a further illustration of true and false in this context these cases From Crockford's The Elements of JavaScript Style are illustrative.

'' == '0' // false    
0 == '' // true
0 == '0' // true

false == 'false' // false 
false == '0' // true

false == undefined // false 
false == null // false 
null == undefined // true

' \t\r\n ' == 0 // true
share|improve this answer
I would not go so far as to say 'almost always' but yes, you should definitely prefer === unless you have a sensible use for == – Erik Reppen Aug 22 '12 at 0:04

I would go get a copy of Douglas Crockford's Javascript the Good Parts. Its probably the best book on Javascript in terms of pointing out the parts of javascript that are good and those tha should be avoided at all costs.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.