Chapter 4. Variables

A variable stores a value in memory so that it can be used later in a program. A variable can be used many times within a single program, and the value is easily changed while the program is running.

First Variables

The  primary reason we use variables is to avoid repeating ourselves in the code. If you are typing the same number more than once, consider using a variable instead so that your code is more general and easier to update.

Example 4-1: Reuse the Same Values

For instance, when you make the y coordinate and diameter for the three circles in this example into variables, the same values are used for each ellipse:

var y = 60;
var d = 80;

function setup() {
  createCanvas(480, 120);
}

function draw() {
  background ...

Get Getting Started with p5.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.