- [Instructor] In the last console.log statement we had two large numbers. What if you wanted to use the resulting number from that operation and subtract 10 from it on the next line? Right now, to be able to do this, we have to type that number again. So, if I wanted to subtract 10 from this result, or see what is 10 minus the result of this is, I need to repeat my operation again on the next line, but this time subtracting 10 from it. And then I will see that this operation, subtracted 10, is equal to this.
This is obviously very wasteful, but luckily another thing that computers are great at is storing and remembering values. Therefore, we can create something called a variable to hold onto that value. In programming languages, a variable is a name that refers to a value. So, we can use a variable name to refer to a value, instead of typing the value again. Here is how it works. We will create a variable called bigNumber, by using the var keyword. Var is the keyword that we need to use whenever we are creating a variable.
After the var keyword, we will give this variable a name, which in this case is going to be bigNumber. So here, I'm going to create a variable called var bigNumber. And I'm going to make this variable equivalent to the value of this. It's important to choose a variable name that makes sense for the current context. In this example, this might not matter too much, but as our programs get more complex, meaningful variable names can help us understand what's going wrong by reading our code.
So naming this kind of a variable that holds a large number as cat, wouldn't make much sense and can confuse other people that might read our code. It might even confuse us, if we are to come back to our code a couple of months later. Programmers always try to make their code as readable as possible. Once this variable is declared, we can assign a value to it by using the = operator. This might seem unusual at first. In math, the = operator is used to signify equality in between two values.
Here, we are using it to do a value assignment to a variable. It takes the value on the right-hand side of the operation and assigns it to the variable on the left-hand side. This is a pretty common procedure that exists in many programming languages. Now that we have a variable that points to our value, we can use this variable name in operations, instead of the value itself. So here, I can console.log the bigNumber, instead of console.logging the additional operation in between those two large numbers, or I can console.log(bigNumber - 10), which is going to give me the same result as these two operations that comes before that.
As you can see, if we don't place semicolons into our program, our program will still work, but it might fail in certain each conditions that can be hard to identify later on. So it's a good idea to use semicolons after every statement, even though it means a bit more typing on our part. Let's console.log the value of bigNumber again, console.log(bigNumber). And let's play this. And as you can see, if you are to enlarge this console a little bit, the value of bigNumber didn't really change after this operation.
If you really wanted to change the value of the variable, then you need to assign a new value to it. So I can have the variable bigNumber here if I wanted to subtract 10 from it, I can do it a couple of ways, actually. What I can do is, maybe I can get this thing, subtract 10 from it, but we were talking about not being wasteful when writing computer programs, so maybe a better way of doing it will be that getting the current value of bigNumber and subtracting 10 from it, so that the final value for bigNumber is going to be the current value of bigNumber minus 10.
And now, if I wanted to console.log the value of bigNumber, you will see that the final value of bigNumber is its initial value, minus 10. So the value of bigNumber actually has changed. To make it even more clear, I can assign a number that is easier to parse, such as three, and then we will see that the final value of bigNumber is three right now, here. And notice how we are not using the var keyword again, when we are changing the value of bigNumber, because the var keyword is used when we are declaring a variable, when we are first creating a variable, but other usages of the same variable wouldn't require the var keyword, you can just refer to the variable by its name at that point.
- Color functions in p5.js
- Operators and variables
- Conditional statements
- Loops, functions, and objects
- Working with arrays