41

I am for the most part a developer in ASP.NET and C#. I name my variables starting in lowercase and my methods starting in uppercase. but most javascript examples I study have functions starting in lowercase. Why is this and does it matter?

function someMethod() { alert('foo'); }

vs

function SomeMethod() { alert('bar'); }
Dominic Rodger
  • 97,747
  • 36
  • 197
  • 212
Bobby Borszich
  • 11,639
  • 9
  • 37
  • 35

5 Answers5

71

A popular convention in Javascript is to only capitalize constructors (also often mistakenly called "classes").

function Person(name) {
  this.name = name;
}
var person = new Person('John');

This convention is so popular that Crockford even included it in its JSLint under an optional — "Require Initial Caps for constructors" : )

Anything that's not a constructor usually starts with lowercase and is camelCased. This style is somewhat native to Javascript; ECMAScript, for example (ECMA-262, 3rd and 5th editions) — which JavaScript and other implementations conform to — follows exactly this convention, naming built-in methods in camelcase — Date.prototype.getFullYear, Object.prototype.hasOwnProperty, String.prototype.charCodeAt, etc.

Community
  • 1
  • 1
kangax
  • 38,898
  • 13
  • 99
  • 135
  • Just wondering... if you were to name constructors starting with a lower case letter, and non-constructor functions starting with an upper case letter, would the resulting code still output anything, or would you get an error? – the12 Feb 06 '17 at 21:45
  • 2
    @the12 As long as you reference the constructors and functions with the same casing it will all be fine. Naming conventions are purely recommendations on how you should write your code. It means that if you're writing code that other people may read over, it is easier for them to follow. – David Moores Apr 25 '17 at 12:06
1

It honestly depends. Your first method is called Camel Coding, and is a standard used by Java and C++ languages, and taught a lot in CS.

The second is used by .NET for their classes and then the _camelCode notation used for private members.

I like the second, but that's my taste, which is what I think this depends on.

Kyle Rosendo
  • 25,001
  • 7
  • 80
  • 118
0

the naming convention with the lowercase at the start is called camel case. The other naming convention with a capital at the start is named Pascal case.

The naming convention only matters for your readability. Pick a convention and remember to stick with it throughout your application.

Andrew Keith
  • 7,515
  • 1
  • 25
  • 41
  • Simple and accurate, I think this is the better answer. Voted up for noting that the approach taken is academic, the code processes regardless of the user preference in structure. – Pete - iCalculator Feb 15 '17 at 08:11
0

A class should always start with a capital letter, because it is easier to read and use:

const data = new Data(); 

Using a capital letter in a function is also easier to read than using lowercase:

User.email 

And strings, int, etc, however, should always start with lowercase to prevent confusion within the code. There is no difference, but it will make it easier to read and understand.

Azametzin
  • 5,223
  • 12
  • 28
  • 46
jerryurenaa
  • 3,863
  • 1
  • 27
  • 17
  • Yet there are no classes at all in Javascript. The latest keyword class in ES2015 is primarily sintactical sugar, as stated [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes). – Nillus Apr 09 '20 at 17:18
  • No, they don't. Classes are not part of Javascript, what you see declared as "class" is just "emulated" to let people used to class OOP paradigm work client-side without having to learn javascript prototype-based OOP implementation. JS classes simply do not exist. – Nillus May 21 '20 at 08:59
-1

I like to think it's because "JavaScript" start's with "java", hence we like to code in the standard of java, while, in it :) At least, this is my reasoning.

I still follow this pattern to this day, even though I program in c# mostly.

It doesn't matter at all; pick which way is most readable for you and your team, and stick with that approach.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105