Beginner JavaScript – Chapter 3 – JavaScript Identifiers
Note:
As stated in my initial post, this is part of a series of JavaScript-related posts. As I am becoming knowledgeable in JavaScript, I am posting out what I’ve learned, to re-enforce my knowledge, as well as maybe helpful other web designers/developers in learning JavaScript. If I have mis-stated anything, please feel free to post it in the comments. You’ll help me, and others in learning JavaScript. Thanks!
JavaScript Identifiers are names; names that you give things in JavaScript. These JavaScript “things” include
- variables
- functions
- objects
- properties
- methods
- events
Like much of JavaScript, there are rules to be followed.
- Identifiers can only contain letters, numbers, underscore (_) and the dollar sign ($).
- Identifiers cannot start with a number.
- Identifiers are case-sensitive.
- Identifiers can be any length.
- Identifiers can not be the same as JavaScript reserved words. (See list of reserved words)
- Don’t use global properties and methods as identifiers (more later).
- Don’t use words similar to reserved words.
When naming an identifier with two words in it, it’s a best practice to use camel case. With this convention, the first letter of each word, excluding the first word, is uppercase. Example:
firstName
myCommonVariableName
Here are some examples of valid identifier naming conventions:
- firstname
- totalPrice
- cust_1
- click_calculate
- $
- $total