← All lessons/Variables & Data Types· Lesson 5 of 32

Declaring variables

Create a named box with let or const, give it a value, and use it to position or size a shape.

About 6 min

A box with a name

A variable is a named box that holds a value. You declare it once, give it a value with =, then use its name anywhere you'd use that value. This lets you write a number in one place and reuse it everywhere.

Use let when the value might change later, and const when it should stay fixed. The name comes first, then =, then the value:

let x = 200;
const size = 80;
Declaring two variables.

Naming

Pick clear names like x, size, or speed. Names can't have spaces — use camelCase for multiple words, like circleSize.

Using a variable

Once a variable exists, you can drop its name into a function call. Here y decides where the circle sits — change the 100 and the circle moves:

A variable positions the circle.

Your turn

Declare a variable named size and set it to 120. Then draw an ellipse centered at (200, 200) that uses size for both its width and its height.

Hints