I’ve been using Javascript for quite a while, but I’ve always ignored the common naming conventions that I see online (camelCase). I know it all depends on the developer’s preference, but why camel case for Javascript?
I’m originally a PHP programmer and I kind of have my own naming convention and I use it everywhere I can, including Javascript. Well, I have to sacrifice my naming convention when it comes to Java though as the program flow depends on those names sometimes.
Anyway, my convention goes like:
var variable_name = 'I always use underscores to separate words';
var another_variable = 'I also tend to use single quotes all the time';
function epic_name_here (some_paramaters ...)
{
// Curly brackets on separate lines
do_something();
}
if (x == y)
{
// Same goes here
}
I have taken this kind of code-writing style from working a lot with phpBB, their docs encourage people to use this style.
Why do I see a lot of people writing Javascript in camelCase? And will it be a problem if I don’t use it?
1
Let’s see what tell two most popular style conventions:
-
JavaScript naming rules in Google JavaScript style guide suggest camel case.
-
Douglas Crockford suggests to start the names with a lower case letter too in JavaScript: The Good Parts. In the same book, Douglas explains that by convention, objects which require
new
start with a capital letter (see page 123 Constructors and new).
why camel case for Javascript?
No reason. This is a common practice, that’s all. Don’t look for a proof that this style is undoubtedly better than any other.
Why do I see a lot of people writing Javascript in camelCase?
Because developers tend to be consistent with other developers who write in the same language. Given that camel case is a common practice, new developers would tend to follow it.
And will it be a problem if I don’t use it?
-
Your code will be inconsistent with code written by other developers,
-
When working in a team, your colleagues may tell you that your style sucks and ask you to change it,
-
If you contribute to an open source project which uses camel case, and you chose a different convention, other contributors will definitely ask you to change your style,
-
Other people using your code may mistakenly use
new
when they shouldn’t, -
You’ll do an additional effort when reading someone else’s code. Other people will do an additional effort when reading yours. Neither you, nor others need to waste time for that,
-
If you use a linter (style checker), you’ll have to rewrite the ruleset to fit your particular style. You could have spent this time to do something useful instead.
1
MainMa said it all insofar as camelCase (from my point of view, underscores remind me of Cobol coding 🙂
Insofar as single versus double quotes: that convention probably migrated from C, where single quotes are used for chars & doubles for strings. In javascript, it doesn’t matter which you use except in such cases as:
var myStr = “He said, ‘She said, it’s dark in here. Where’s Kevin’s flashlight?'”
Because the single quote is used for inner quotes, apostrophes and contractions, it makes more sense to use double quotes for the string assignment. Doing it the other way would require escaping, at the least, the contraction.
1
According to this study:
results indicate a significant improvement in time and lower visual
effort with the underscore style. The interaction of Experience with
Style indicates that novices benefit twice as much with respect to
time, with the underscore style.
so if that matters to you go ahead with underscore style.