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?
- can someone give me tips to draw an arc I always get confuse when I try to draw an arc(2 votes)
- What I do when I have no idea where to put anything, i just make everything 200, then I just number scrub.(7 votes)
- Do you use a capital letter in the middle of variable names, like eyeSize, for a specific reason? Or is it simply preference?(38 votes)
- It's simply a readability convention.
Older programming languages like FORTRAN did everything in upper case, and it made readability a real b*h. eyeSize is so much easier to read that EYESIZE, or even eyesize.
Thank goodness we're no longer limited to uppercase only, and 80 columns to a line. And "Face Down, Nine-edge first" is no longer a thing.(3 votes)
- The fourth bullet says variable names should be meaningful, but abbreviating is easier, what harm could it cause?(2 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!!(5 votes)
- Can khan academy put a python course too.(15 votes)
- Why is it that "$" or "_" are allowed when typing variable names while other symbols are not?(0 votes)
- lets say you wanna name a variable *star
it shouldnt work because * is a reserved symbol in mostly every programming language to multiply something by something.
the computer is too dumb to tell whether you:
i. are calling the variable (*star) or
ii. you wanna multiply something to a variable called star (as in 10 * star)
Either way, the conventional way to name variables is to use camelCases and avoid all sort of symbols altogether. Its really fine to name variables whatever you want in a simple program, but as projects gets more and more complex the readability of the codes becomes very important.
I believe there are other reasons as well and please correct me if im wrong(18 votes)
- Can you give an example of when repeating "var" for x could cause unforeseen problems when programming?(15 votes)
- As you get into animations and functions, the misuse of
var
is catastrophic. In
why won't the ball move?var x = 25;
draw = function() {
ellipse(x, 200, 40, 40);
var x = x + 1;
};(2 votes)
- Can you give an example of why I should use '$' in front of the variable name??(5 votes)
- There are other languages (like UNIX shells) where obtaining a variable's value requires prefacing the the variables name with
$
, e.g.
Javascript attempts to make porting from other environments easy, so it naturally would accommodate those languages.PATH=$bindir:$PATH
If you take thejQuery
tutorials, you will discover that it refers to itself as$
.
Should you use$
? Probably not.(8 votes)
- Variable names should use "camel case" for multiple words, like "toothSize" instead of "toothsize"
1) what does "camel case " mean
2) what will happen to the program if the case is not uppercase on the second part of the variable command "toothSize" "toothsize"(4 votes)- Camel casing is when you capitalize the first letters of the next words justLikeThis. I don't think anything will happen to the program. It does makes it easier for you to read though.(2 votes)
- is there an "infinite" Amount of Variables that are able to be use, and is there some variables that are not able to be use?(5 votes)
- Theoretically, you can create unlimited variables. But there are pre-defined and reserved keywords unable to turn into variables -- like
camera
for example (not in a function), or evenObject
. There are plenty of other keywords already reserved so watch out! 👍:D(5 votes)
- Why would u want to do toothSize instead of toothsize for the variable name(5 votes)
- let's see ... ...
1. asVariableNamesGoLongerItGetsHarderToRead
2. as_variable_names_go_longer_it_gets_harder_to_read
3. AsVariableNamesGoLongerItGetsHarderToRead
Wondering if we could do this -hello$world$
PMP - P ractice M akes P erfect
try this:https://www.khanacademy.org/computer-programming/errored-vars/6451454895226880(5 votes)