Have you ever noticed that you have used a certain number value for something over and over again? Let’s say that you want to make a graphic with circles that are all 10 pixels wide and tall. You could keep typing 10 over and over and try to find each circle when you want to change the size to 20 pixels later. Or you could use something variables.
Variables allow you to assign a name to a number value. So in the case of the circles, you could assign 10 to the variable circleSize. The name can be as long as you want, under one condition. The first word must be all lowercase and every word after that has to start with an uppercase letter. You can also use abbreviations, just make sure you remember what they mean. Or you can label them with comments.
Variables can also be used as coordinates. Let’s say you want to make your first row of circles have the y-coordinate of 50. You could call this value, line1. Or if you wanted them the all line up the other way, call it column1, and put it into the x-coordinate slot.
The only restriction to variables is color. But, you can use it to store values for coordinates, width, height, and text. If you are going to make a variable for text, make sure your message is enclosed in quotation marks.
To make a variable, type var variablesName = variable value; Make sure you have a space between the var and the name or else you will get an error message. I always put my variables at the top for they are easy to find and change later. And don’t be alarmed if the var changes color, it’s supposed to. They should look like this:
You can also redefine a variable. This would come in handy if you wanted the second half of your circles to be 20 pixels while the first half is still 10 . Once you get to a section you want to redefine, simply type the variable’s name = new value; This will change that value for the rest of the code. You can also redefine it as many times as you want.