[ACCEPTED]-What is the best-practice casing style for javascript? Why?-casing

Accepted answer
Score: 40

I prefer PascalCase for constructors and 6 camelCase for everything else. That's the 5 style that JS standard library uses and 4 well... every JS framework I've seen so 3 far :)

And I use all_lowercase naming convention 2 for all files served from web. There are 1 some case-insensitive file systems out there.

Score: 16

The core language uses InitialCaps for constructors 12 (e.g. Object, Date, Number, RegExp) and 11 camelCase for methods and properties (e.g. something.toString(), quantity.valueOf(), regexp.ignoreCase). This 10 convention is also followed in the DOM specifications 9 and implementations (e.g. HTMLElement.setAttribute()). So 8 it makes the most sense to adopt the same 7 convention, or you finish up with a horrendous 6 mishmash of styles like:

var number_of_fish_requested = document.getElementById("fish").value;
var fish_count = parseInt(number_of_fish_requested, 10);

which just becomes 5 utterly confusing, not only to type but, much 4 more importantly, to read.

(You spend more 3 time reading code, trying to debug or modify 2 it, than you ever do writing it in the first 1 place.)

Score: 2

What I have seen so far is a very large 30 diversity of casing standards.

As far as 29 I'm concerned, I use C# styling for writing 28 my JavaScript code. I use classes a lot 27 (well functions as classes, and usually 26 don't have independent functions.) So, I 25 use PascalCase for class names, public methods, properties 24 and all global variables and camelCase for 23 arguments, local variables and private functions. This 22 somehow reflects my common environment, helping 21 to distinguish the variable scopes. I also 20 tend to keep my class functions in a separate 19 file with the same name as my ClassName 18 (ClassName.js, ClassName.min.js).

This was 17 about my approach.

I also noticed that Java 16 programmers, follow the Java rules (and 15 the writing style resembles the Java language.) Ruby 14 on Rails programmers follow their own naming 13 standards such as underscore_separated_var_name.

Further, as you mentioned, there 12 is a tendency to use pascalCase a lot in 11 naming in very popular frameworks whose 10 authors come from different communities 9 like Linux/Open source community and Microsoft 8 developers (jQuery, knockout.js, JSJaC, etc.)

I 7 should note that none of these methods are 6 wrong or right, when it comes to JS. The 5 primary purpose of your naming conventions 4 and file structuring is the readability. If 3 you are consistent then you in future and 2 your fellow developers will quickly understand 1 and get on with your code.

Score: 1

I prefer camelCase for everything except 6 for constructors. The reason (and I believe 5 this is why Mr. Crockford suggested this 4 as well) is because in other languages, such 3 as Java, the convention is capitalize your 2 classes, which is what constructors are 1 used for.

That is my $0.02.

Score: 1

All lower case with underscore separators 5 is the easiest to read; it follows natural 4 language. "Best" will get you in to a holy 3 war; the reality is case doesn't matter 2 as much as other design issues but it's 1 an easy topic to polarize.


More Related questions