Javascript Tutorial #2.3 - Reassinging (Changing) Variables

Using variables is all well and good, bet they're not that useful if you can't change them. Here are some ways to alter the variables:

The first way is to write the following:
<name> = <newValue>;

where <name> is the name of the variable and <newValue> is the name of the value you want to change the variable to. This is the most commonly used way.

However, if your variable is a number, there are a few shortcuts:

1. Changing a number by a certain amount:
 If you have a variable called numeral, and you want to multiply it by 5, instead of typing
numeral = numeral * 5;
you can type 

numeral *= 5;
instead. This will save a lot of space in your code. This not only works for multiplication, but for division, subtraction and addition and multiplication.

You can do better. If you only want to increase or decrease your variable by 1, you can write
numeral++;
or
numeral--;
of course replacing numeral with your variable name. 

CHALLENGE: Create a few variables using both let and const and try and reassign them with the 3 different methods mentioned in this post. Notice the errors that const gives you when you try and reassign it.

If you have any questions, let me know in the comments section.
- Noah

Comments