FAQ: Variables - The Increment and Decrement Operator

okay so basically 3 ** 3 works as a cube where 3 * 3 works as a square.

so what about 3 *** 3 would that work as 81?

or

3 ** 4 = 48?

The more multiply symbols are increasing the more multiplication is increasing?

no, the operators are limited to what is implemented in Javascript, *** is not implemented. So that won’t work

yep

Oh great, Its amazing actually. Thanks for the advise.

Why would you use the increment or decrement operator as opposed to a Mathematical assignment operator that does +1 or -1… like += or -=?

because the increment operator also includes pre- and post-increment. Can be very useful for loops

What’s the difference between the += and ++ operators?

+= allows us to increment by however much we wish:

x += 5

while ++ is a short-hand to increment/increase by 1. This is such a common operations (loops and more)

Let’s say that you’re making it super cooker game where you could click the Sprite and then it changed to variable by one and what is it you can make it even more faster for the coach run faster is my writing the code like this.

Clicks++;

or you’d have to completely for a longer line of code that would cause it to just change it by 1 but writing it like this above makes it much faster and easier to write.

Something to notice. If you try to execute this within the console.log parenthases, you do not see any changes to the value.

let lostDollar = 3; console.log(lostDollar++); // No changes lostDollar++; console.log(lostDollar); // Increases to 5

This is because lostDollar increments by one after its value is logged to the console. You see this in action as the first log prints 3 and the second prints 5, demonstrating that lostDollar has been incremented twice as written in your program. If you wanted to increment lostDollar and log the incremented value at the same time, you can use ++lostDollar, which will increment the variable before printing its value.

So, the value does actually change, it’s just that the value changes after you log it.

1 Like

makes so much more sense now haha. I was wondering why it ended up with 5 as well at one point too. Muchas gracias!

1 Like

If you want to increase the variable by more than 1,using addition, how would you write that if (x++) increases by 1
let x = 10;
and you not dividing or multiplying?

++ is just a short-hand for incrementing by one (Given how common this operation is). you can simple use addition (+) with (re-)assignment (=) to increase by any desired value

The lesson should consider adding information about using the increment and decrement operators before or after the variable name. For example:

let x = 10; console.log(x++); // Outputs 10, then increments to 11 let y = 10; console.log(++y); // Increments to 11, then outputs 11

In which cases is the increase and decrease used?

Since the increment and decrement operators only change by 1 or -1, respectively, it follows they are very useful for unit counting, either up (counting sheep) or down, (countdown, for example).

Either way we are following the number line, from left to right, or right to left. It is the situation we have set up that will dictate the direction we take.

The most common use of these operators is in loops, such as for and while. In the for loop the most common usage is iteration of strings and arrays. They are convenient for that purpose, but we’re not confined to that, either.

Say we want to build a table of values for our slope equation.

// here we define the constants in our function
// m => slope of the line
// b => y-intercept
const slope_factory = function (m, b) {
    return x => m * x + b // return a f() with the constants fixed
}
// here we create the user function
let f = slope_factory(1.5, 1.5)  

Now we have a constant function that we can feed all our values for x, the independent variable and compute y, the dependent variable. The two together make up the coordinate of one point on the line produced by that line equation. Above we used let so that we can re-use that variable for another equation once finished with the first one.

 > for (let x = -5; x < 6; x++) {
 >     console.log(`(${x}, ${f(x)})`)
 > }
// f(x) = 3/2 * x + 3/2
(-5, -6)
(-4, -4.5)
(-3, -3)
(-2, -1.5)
(-1, 0)
(0, 1.5)
(1, 3)
(2, 4.5)
(3, 6)
(4, 7.5)
(5, 9)

This is but one example, but before even the end of this course you will have encountered dozens of applications, if not hundreds.

1 Like