If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

Main content

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?

  • blobby green style avatar for user rishabh mahanta
    can someone give me tips to draw an arc I always get confuse when I try to draw an arc
    (2 votes)
    Default Khan Academy avatar avatar for user
  • spunky sam blue style avatar for user Kyle Severson
    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)
    Default Khan Academy avatar avatar for user
    • aqualine ultimate style avatar for user Rich Demanowski
      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)
  • hopper jumping style avatar for user Kevin Zhao
    The fourth bullet says variable names should be meaningful, but abbreviating is easier, what harm could it cause?
    (2 votes)
    Default Khan Academy avatar avatar for user
    • leaf blue style avatar for user Daniel
      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)
  • blobby green style avatar for user ageddonkey
    Can khan academy put a python course too.
    (15 votes)
    Default Khan Academy avatar avatar for user
  • starky sapling style avatar for user Celine
    Why is it that "$" or "_" are allowed when typing variable names while other symbols are not?
    (0 votes)
    Default Khan Academy avatar avatar for user
    • blobby green style avatar for user Bray O.
      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)
  • hopper jumping style avatar for user David Wendell
    Can you give an example of when repeating "var" for x could cause unforeseen problems when programming?
    (15 votes)
    Default Khan Academy avatar avatar for user
  • aqualine seed style avatar for user Koi Lunstrum
    Can you give an example of why I should use '$' in front of the variable name??
    (5 votes)
    Default Khan Academy avatar avatar for user
    • old spice man green style avatar for user Bob Lyon
      There are other languages (like UNIX shells) where obtaining a variable's value requires prefacing the the variables name with $, e.g.
          PATH=$bindir:$PATH
      Javascript attempts to make porting from other environments easy, so it naturally would accommodate those languages.

      If you take the jQuery tutorials, you will discover that it refers to itself as $.

      Should you use $? Probably not.
      (8 votes)
  • leaf blue style avatar for user Darrell
    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)
    Default Khan Academy avatar avatar for user
  • aqualine ultimate style avatar for user Jared Falci-Pena
    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)
    Default Khan Academy avatar avatar for user
  • winston baby style avatar for user Rami Ibraheem
    Why would u want to do toothSize instead of toothsize for the variable name
    (5 votes)
    Default Khan Academy avatar avatar for user