3:1 Using variables
A variable is a named thing that can store a number, text and some other stuff.
They are the fundamental way we change and affect things with code.
JavaScript Variables
JavaScript has a number of ways of creating variables.
var
let
const
We used to use var but these days we use let to create variables that can change.
const makes an unchangeable variable, we aren't going to concern ourselves right now with const!
You will see var a lot in older tutorials - it still works, but isn't used much these days.
Lets create a variable
When you first make a variable you use the word ..
This lets JavaScript know we're making a new variable. After that point we don't need to put infront of the variable again (unless we make a new variable). infront of the variable again (unless we make a new variable).
let xPosition = 30;
We just made a new variable called "xPosition" and gave it a value of 30
What do we do with them
You donβt have to assign a value when you make a variable, that can be done later:
let xPosition;
Then later in the code:
Things to look out for
Some variable names are protected.
This means that they're already in use by the system. The system could be your browser, or it could be a library like p5.js.
window is used by all browsers.
width, circle, draw and many others are used by p5.js.
So you need to be bit careful when naming variables (p5.js will let you know if there's a problem in the console).
How to name variables
Variables are case sensitive (i.e. they care about capitals), variables can't have spaces - there's some good guidance here:
https://www.w3schools.com/js/js_conventions.asp
camel case is most commonly used. This involves using a capital letter for the start of each word in a variable with no spaces and a beginning with a lowercase letter. Think of a camels humps!
π«
for instance:
let myCamelName = "Bernard"