*don't listen to every suggestion of Crockford though; e.g. having to hoist your variable declarations is as obnoxious an idea in JavaScript as it is in C
*don't listen to every suggestion of Crockford though; e.g. having to hoist your variable declarations is as obnoxious an idea in JavaScript as it is in C
I usually find the people that complain the loudest about JS have been burned by their own ignorance.
I'll admit the learning curve for basic JS syntax can be a little high, but reading through and understanding most of Crockford's book will get you 80-90% of the way there. The rest is either ES2015 or framework-specific -- both of which build off of basic JS.
A good software developer learns constantly.
He recommends hoisting the declarations because that's how `var` scope works in JavaScript. Normally you'd want to introduce variables where they're actually used to provide context for them.
A variable definition at the beginning of a function implies the variable is important to understand the general flow of the function. This can be especially misleading for temporary variables like counters which only matter within a particular loop.
Additionally assigning values halfway into the function implies they had different values before, leading you to go back and check whether they were assigned anything else (and used for anything else) or just declared without a value.
Of course these problems can be avoided by simply using the new let/const variables which are block scoped, but even here Crockford would likely advocated for hoisting within the same block because of the Temporal Dead Zone (i.e. a block scoped variable exists from the start of the block but referencing it before it was declared is an error).
Of course you don't have to follow all his rules of what are good/bad parts, some of which feel a bit Byzantine at times, but he has a point and usually for a very good reason.
Check out the JsLint Google+ page for his views on more recent additions