That is when binding or variables come in.
let num = 4 + 3;
The special keyword
num and an
num. In this case, we want to add
3. The total is now tied to the
Instead of writing
4 + 3 multiple times in your program, you can mention only
num. If you want to change the
+ operator to a
* and multiply it, you can go to the original mention of the variable and change it. This is better than writing
4 + 3 multiple times in your code.
You can update
num by adding another variable to it:
let inches = 20;
console.log(num + inches);
num + inches isn’t tied to a variable, the value is gone once the program is done. If it was attached to a variable, you could reuse that variable elsewhere in the code. As long as you mention the variable before you use it.
Uncaught ReferenceError: [variable name] is not defined
When a variable has a value, it remains with it until the variable is updated to something else. You can use the
= operator any time to bind an existing variable to another value. This will disconnect the variable from whatever old value and update with a new one.
So if later down the program, I change
let num = 4 + 7;
num = 50;
num no longer equals to
7. It is now
You can also update
num by taking its current value and subtracting a few values from it:
num = num - 2;