Main content
Computer programming
Review: Variables
Here's what we covered in this tutorial on variables:
A variable is a way to store values. To use a variable, we must both declare it—to let the program know about the variable—and then assign it—to let the program know what value we are storing in the variable.
Here's how we would declare a variable named "xPos":
var xPos;
Now, we can assign xPos to hold the value 10:
xPos = 10;
If we want to—and we often do!—we can declare and assign in one statement:
var xPos = 10;
If, for some reason, we want to change the value of a variable later, we can re-assign it:
var xPos = 10;
// some time later ...
xPos = 20;
We'll soon see why re-assignment can be useful when we want to animate our drawings.
How can we pick names for our variables? For variables in JavaScript, follow these rules:
- Variable names can begin with letters, or the symbols $ or _. They can only contain letters, numbers, $ and _. They cannot begin with a number. "myVariable", "leaf_1", and "$money3" are all examples of valid variable names.
- Variable names are case sensitive, which means that "xPos" is different from "xpos", so make sure you are consistent.
- Variable names can't be the same as existing variable names, and there are a lot in our ProcessingJS programming environment. If you ever see an error pop up like "Read only!", try changing your variable name.
- Variable names should be clear and meaningful; for example, instead of "ts", use "toothSize".
- Variable names should use camel case for multiple words, like "toothSize" instead of "toothsize" or "tooth_size".
We'll use variables a lot when we learn animation in the next tutorial, so ask questions here if you don't understand something about them.
Want to join the conversation?
- So can you make a variable called var?(835 votes)
- Nope, you can't. var is a reserved word for using variables in JavaScript. You can't use it as a name of the same thing.
There are also some more words you cannot use for making a variable. Some can be found here:
http://www.javascripter.net/faq/reserved.htm(798 votes)
- High concentration is really needed as things are getting harder.Sometimes longer time is used to figure out variables.Who else also has the same situation with me?(510 votes)
- I agree. There should definitely be more focus on fundamentals before piling on more advanced stuff. There is way too much confusion about very rudimentary things(316 votes)
- What's Camel case?(104 votes)
- camelCaseIsWhereYouCapitalizeNewWordsInAVariableOrFunctionExceptForTheFirstOne.
snake_case_is_where_you_put_an_underscore_for_each_new_word.
PascalCaseIsLikeCamelCaseExceptTheFirstWordIsAlsoCapitalized.(586 votes)
- can someone give me tips to draw an arc I always get confuse when I try to draw an arc(167 votes)
- You can think of the six parameters that arc uses as three sets of two numbers: center, size, and segment. The first numbers are the x and y for the center of you arc's ellipse. The size is defined by width and height of your ellipse. Finally, the segment has two number for the beginning and end of the arc, and are in units of degrees around the ellipse. For me, the hardest thing to remember is that positions around the arc ellipse start at 3 o'clock and continue clockwise around the ellipse.(287 votes)
- Why is it that "$" or "_" are allowed when typing variable names while other symbols are not?(99 votes)
- Because someone decided it should be that way.(383 votes)
- Are all rule different when applied to Java or other programming software(49 votes)
- Javascript is one of many programming languages. Each programming language has its own specific rules.(112 votes)
- Do you use a capital letter in the middle of variable names, like eyeSize, for a specific reason? Or is it simply preference?(39 votes)
- Capitalizing like that is called Camel Case, and it's used for variable names simply because it's an efficient and widely accepted way to name your variables, since you can't put spaces in your variable names, and variableOne is a lot easier to read than VARIABLEONE, variableone, etc.(113 votes)
- why are semicolons so important in coding?why can't you use a comma(37 votes)
- Semicolons are used in JS to separate JS statements, such as variable declarations, assignments, and function calls that are on the same line. They tell the interpreter that one statement has been completed and the other is starting. The KA ProcessingJS environment requires them after every line, though.
Commas and semicolons are not interchangeable in the same way the words wallet and gallbladder are not interchangeable. "I lost my wallet" and "I lost my gallbladder" have two very separate meanings.(112 votes)
- The fourth bullet says variable names should be meaningful, but abbreviating is easier, what harm could it cause?(10 votes)
- Putting in other words what the previous person said, your variable name does not HAVE to be meaningful, but using actual words and capitalizing first letters could greatly help you, as well as anyone else who would want to make a spin off of your code!!(12 votes)
- Can khan academy put a python course too.(17 votes)