Lesson 1: Strings
In programming, a variable is a container that holds information. You can name the container and store something inside it, like a number or a word. When you need that information later, you can use the variable’s name to get it.
Think of a variable like a sticky note. You can write something on it, stick it somewhere, and look at it when you need to remember what you wrote.
Here is an example of a variable in JavaScript. This code creates a variable called message and stores the word "Hello!" inside it. Hover over each part of the code to see what it does.
Once a variable has been declared, you can use it by referring to its variable name. You can also change its value using the = sign. Note that you don't need to use the var keyword again when changing the value of a variable. The var keyword is only used to initially declare the variable. Below, we change the value of the message variable to "Goodbye!".
Create a variable called color and set it to the color you want the ball to turn. You can choose a color by writing its name inside quotes, like "blue" or "red". See this page for a list of colors to choose from.
If you're having trouble with this exercise, here are some common mistakes and hints to help you out:
| Mistake | Hint |
|---|---|
| Using the the wrong keyword to declare the variable (e.g., let or const). | In these exercises, you should use the var keyword to declare the variable so that the automated checker will work. |
| Forgetting to use quotes around the color name (e.g., var color = green;) | When the quotes are missing, JavaScript thinks you're trying to refer to a variable called green when you are actually just trying to set the value "green" as the contents of the variable color. |
| Making a minor typo (e.g., bar instead of var or collor instead of color). | Check your spelling and make sure you use the correct variable name. |
| Using an unsupported color name (e.g., let color = "rainbow";) | Only certain colors work. Try a common color like "blue", "red", or "yellow". |