Lesson 5
Naming Rules
Good variable names make your code easy to read and understand. Follow these rules to name your variables in JavaScript.
userNamecamelCase (recommended)user_namesnake_case$priceStarts with $_countStarts with _total2Can include digits (not at start)
2userCannot start with a digituser-nameHyphens are not alloweduser nameSpaces are not allowedletCannot use reserved keywords@valueSpecial characters not allowed
- 1Can contain letters (a–z, A–Z), digits (0–9), _ and $
- 2Cannot start with a digit
- 3Case-sensitive (myVar and myvar are different)
- 4Cannot use reserved keywords
- 5Should be meaningful and descriptive
These are JavaScript keywords. You cannot use them as variable names.
letconstvarfunctionreturnifelseforwhileclassnewswitchcasebreakcontinuedefaulttrycatchfinallythrowtypeofinstanceofinofCheck if the following variable names are valid.
| Variable Name | Valid? |
|---|---|
firstName | |
1stName | |
user_name$ | |
user-name | |
let |
Use camelCase for variables. It is the most common convention in JavaScript.
let totalAmount = 250;Try creating valid variable names.
JavaScript
