Browsed by
Tag: beginner

2 hours of Video on JavaScript from Douglas Crockford

2 hours of Video on JavaScript from Douglas Crockford

I spend a lot of time doing web development in my daily life. I am employed as a software engineer, and as part of that employment, I get the opportunity to program in JavaScript from time to time. I’ll be the first to admit that I am not great at it, as the language was never formally “taught” to me. Because of this, I have found several interesting parts of the language that have caused me some problems over time. Below are several links to videos containing instruction from Douglas Crockford on the do’s and dont’s of the JavaScript language, which I found very informative, given the problem I ran into a while ago on the project I am working on.

The problem I had to solve was introduced by another programmer, and that is: he did not formally declare a variable, but instead just started using it. Obviously, I did not discover this until much later, however. Unfortunatly, this variable was set up as a counter, and because the name was generic, it was used in several functions to accumulate the count within different functions. The person who originally programmed it did not know any better, and probably thought that while odd, since javascript allowed him to use the variable without issue, that it must be ok.

Well, it turns out, I got the task of debugging this particular code, which was throwing some sort of “index out of bounds” error that no one could find. After many days of beating my head against the problem, it turned out that instead of declaring var i = 0; in some places, “i” simply came in to use, and was never formally declared. When I found this problem, the var declaration added to the variable fixed the problem, but I never really knew what was going on in the first place to cause the problem, because the error was showing up in different methods, which didn’t make sense because of my presumptions about scope in JavaScript.

It turns out, if you don’t formally declare a variable in JavaScript, but instead just start using it, the scope for that variable is global. I discovered this only recently, after watching a great series on JavaScript by Douglas Crockford. The series is available via Yahoo!’s video service, and is entitled “The JavaScript Programming Language”.

The series is about 2 hours in length, and covers a vast amount of JavaScript nuances and gotchas, and is highly recommended for even the seasoned JavaScript programmer. Douglas Crockford also is the author of a very cool tool you may be familiar with, called JSLint. This tool is very interesting, in that it will tell you more about your JS programming habits than you ever wanted to know. It is a great tool, and I highly recommend investigating these tools to help you become a better JS programmer.