CRCD.T1.M2.02: Variables
Introduction to Variables
Variables
Vocabulary
New Code
Declare and assign a value to a variable
Declare a variable
Naming Variables
Rules for Naming Variables
Variable labels should be meaningful, but you can choose almost any label you like. There are just a few rules to be aware of.
CamelCase
Labels with multiple words can be easier to read in camelCase. A camelCase label looks like sizeOfRectangle
or aReallyLongLabelName
. The first letter of the variable name is usually lower case, each new word starts with a capital letter. This helps you see the start of new words without using spaces, which are not allowed in variable names.
Naming Rules
There are a few rules when choosing labels:
- Labels cannot include spaces. For example,
width of rectangle
would generate an error.- width of rectangle becomes widthOfRectangle
- Labels cannot begin with a number.
4sides
and2morrow
will generate errors.- 4cat becomes fourCat
- Labels CAN end with a number
- cat 4 becomes cat4
- big dog 2 becomes bigDog2
- Be very careful with spelling. If labels are not spelled exactly the same way, the computer will not realize that they refer to the same variable.
- yLocation is NOT the same as YLocation
- Labels are case-sensitive.
size
is not the same asSize
orSIZE
.- cat, CAT, and Cat are 3 different variables (even if 2 of them are named incorrectly!)
Want to Practice?
Naming Variables Practice Sheet
Naming Variables Practice Sheet Answer Key