Javascript: Always define variable with ‘var’

So today, I found one more unseen behavior of Javascript (at least for me).

I wrote a very simple function in Javascript and forgot to add ‘var’ in front of variable definition. This function worked first time but for second time I got JS error saying the function does not exist. I was like what the hell, where did my function go? It just worked as expected for first time and suddenly browser is not able to find it.
It took me 20 minutes to find out the reason of my biggest mistake of tonight. Such a waste of precious 20 minutes at 3:00 AM :(
Lesson learned, always define variables in Javascript with ‘var’ keyword or else stay up till late night ;)

Leave a Reply

Your email address will not be published. Please enter your name, email and a comment.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">