Variables
A variable serves as a virtual container that we can store data in. This data can take the form of numbers, strings, entire objects, etc. You can put almost anything into a variable, and in fact for many of the lessons in the later half of his course you will need to. By storing something inside a variable you are essentially giving it a name that the code can refer to that item or value as. It is this feature that lets us reference the objects we make and adjust the various parameters, and why we need to store things like our synthesizers and audio effects inside of a variable when we create them.
In some older javascript codes you may see a variable with the keyword var
instead of let
. Javascript updated this recently, but still supports using var in your code. This page provides a list of everything you need might need to know about variables, but still uses the keyword var.
If you see the keyword const
, it will function exactly as a variable, except the value will not be able to change while the code is running. This is a constant value. We will mostly see this during the Physical Computing Unit.
One way to think of a variable is like an excel spreadsheet. You can store data and refer to that data by the cell that it is located in. Then, if you update the information inside of that cell, the math referring to that value will be automatically updated.
The picture below will update items in column F based on changes to other cells in the same row.
A video demonstration of variables from The Coding Train: