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

Teaching guide: Intro to JS - Variables

This is a teaching guide for the Intro to JS lesson on Variables.

What the student will learn

  • How to define variables to store numeric values in their program.
  • How to reassign variables to new values later.
  • How to pass variables as parameters to their functions.
  • How to include variables in math expressions.
  • The rules for making up good variable names.

The student will be able to write code like:

Where students struggle

  • Students sometimes ask what the point of variables is - why it’s worth it to go through the effort to learn the new concept. The very next lesson will show that you need variables to make animations, which are one really fun use of variables. Generally, variables enable you to make programs that are more complex than a drawing and can even make drawings easier to program, because the variables can store repeatedly used information. Plus, naming your variables allows you to communicate what the different numbers are used for in your program, making it easier for other people to understand and modify it (including yourself in the future!).
  • Students might accidentally declare their variable after they use it. They’ll get an error if they do that, which should help remind them that variables must be defined first, and then used.
  • Students sometimes give their variables invalid names, like a name with a space in it or a disallowed character. Remind them to re-read the variable naming rules in the review article.

Additional materials: Discussion questions

These are questions that you can ask students individually after they've done the lesson, or lead a group discussion around, if everyone's gotten to the same point.
  • Ask them about a favorite game and open it up. Prompt them to brainstorm what variables they think the game uses - what data the game has to store. For example, many games have a variable to store the player’s score, or a variable to store the current level.
  • Ask them to look at their first project, the one they programmed before they knew about variables. Is there anything they'd use variables for now? For example, for the dinner plate project, a student might program multiple grapes, and they could now use a variable to store the grape size.

Additional materials: Trivia questions

These can be fun to do as a class after everyone’s gotten through the lesson. They can also lead to discussion about which questions are the hardest. Play them on Quizizz.

Additional materials: Unplugged activities

An "unplugged" activity is one that you can do with students without needing to use a computer at all. They can help convey concepts in a more visceral way, and they can also be a backup activity for when computers fail.
Code.org offers a Variables in Envelopes activity (see lesson plan and video). It doesn’t align perfectly with this lesson because the variables in it often hold string data types, not numbers. We teach students how to store strings later. However, the concept of storing data is the same.

Want to join the conversation?