Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Working With Numbers Doing Math

w Verhoef
w Verhoef
8,632 Points

Why are you adding capital letter to your variables? For example var secondsPerMin=60; Is there any reason for capitals?

Why are you adding capital letter to your variables? For example var secondsPerMin = 60; you give the P and the M a capital letter what is the function of capitalizing in a variable name?

3 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

Actually, these are not constants. Constants in JavaScript can take any legal identifier, and as is the common practice in other languages, it's easiest to use CAPS when naming constants so we know the difference between them and normal variables. The only difference between constants and variables is that constants are declared like this:

const CONSTANT_NAME = 10;

while variables are declared like this:

var variableName = 5;

Notice the only real difference is the use of leading const and var keywords.

What the original question is asking, I believe, is about the use of camelCaps. CamelCaps are a convention for naming variables in Javascript (and other languages, I assume). This is done because it is easier to read. Sometimes variable names are short or one word, like x or sum, but when you need a more descriptive variable name like currentSum or playerOneCurrentScore, using camel caps makes identifying and reading them much easier.

For example, the following two variable names are identical and are both allowed in JavaScript, but the second is much easier to read in my opinion:

var currentuserlastnamewithtitle = "Mr. Field";
var currentUserLastNameWithTitle = "Mr. Smith";
w Verhoef
w Verhoef
8,632 Points

Hey Ryan,

Thanks for you answer. That was exactly my question. So it's done because it is easier to read.

Addison Ulhaq
Addison Ulhaq
3,659 Points

camelCase is the naming convention that has been adopted by the javascript community as the preferred method of writing identifiers such as variables. PascalCase has been adopted by C+ programmers as their writing convention... it's really just preference on the coder's part, but I would stick with the preferred/adopted conventions in order to avoid getting flack from your peers/coworkers if you hand off code and it's written in a convention that's not typical/industry standard.

w Verhoef
w Verhoef
8,632 Points

Okey great, good to know! Thanks everybody for the answers!:)

Shadab Khan
Shadab Khan
5,470 Points

Hi Verhoef, the above variable naming convention is called the 'camelcasing' naming convention. It is called so because it looks like the humps on the back of a camel. Camel case is also in my experience one of the most used variable naming conventions. I hope that helps. Cheers.