Variable names in JavaScript

Names can be made up of letters, numerals, underscores and dollar signs but they cannot start with a numeral. For the moment, start all your variable names with a letter. Don't start with a dollar or underscore. The browser will accept them but programmers tend to reserve such names for particular purposes.

Camel case

The convention in JavaScript is to start with a lower case letter and to mark the start of each word within the name with a capital letter:

userName
firstName
lastName
dateOfEnrolment

But please not:

surName
dateOfEnrolMent

You only insert capital letters if the variable name is made up of several words.

This convention is known as "camel case" because of the humps in the names.

Fonts

Whilst we're on conventions, program editors always use a plain font like Courier New or Lucida Console where all the letters are the same width. It's usual to use these fonts too when you're quoting program code. If you use a fancier typographic font then you risk confusion within variable names like these:

weekAlI
weekAI1

Are those final characters ones or capital "I"s or lower case "l"s? There's no confusion in Courier:

weekAll
weekAI1

Meaningful names

You should always use names that are meaningful. This program would have worked equally well if the variable had been named "x":

x = prompt("What is your name", "")
alert("Hello " + x + ".")

Try it. Try some more names and see what works.

The program runs with x as the name of the variable but don't forget that a program in the real world might exist for many years. At the moment, you know what "x" means but you (or somebody else) might be coming back to work on this program in five years time and they won't have a clue what was meant by "x". You must use meaningful names in programs. They'll take a bit longer to type but you'll save that time over and over again.

Especially, please do not use funny or abusive names. The joke wears off rapidly. Also do not use the names of your current heroes. Who were roger, john, keith, and pete and what data is being held in these variables?

MS Access technical tips

Visual FoxPro technical tips

General Tips

 

More tips from Alvechurch Data

Search page for Alvechurch Data

Searching a web site with JavaScript

Read More

Site Map

Site map of the Alvechurch Data site

Read More

Javascript while loops

JavaScript do and while loops

Read More

Javascript for loops

Javascript for loops

Read More

Repetition in Javascript

JavaScript structures for repetition

Read More