Javascript Naming Convention [closed]

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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
}
</code>
<code>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 } </code>
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:

  1. JavaScript naming rules in Google JavaScript style guide suggest camel case.

  2. 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?

  1. Your code will be inconsistent with code written by other developers,

  2. When working in a team, your colleagues may tell you that your style sucks and ask you to change it,

  3. 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,

  4. Other people using your code may mistakenly use new when they shouldn’t,

  5. 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,

  6. 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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật