Skip to content
Intro to JS+prossecingJS
Share
Explore
Session 2

icon picker
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;
Not only you can store numbers, you can also store strings too in variables.
var size = 20;
var face = 1.5;
var message = "Hello";
Note: The code reeds from top to bottom so you have to be careful of your variable.

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.