Shorthand operations in JavaScript

You can perform some operations on the current value of the variable, and then write the result back to the same variable. See an example:

let num = 1; // declare the variable num and write the value 1 to it num = num + 2; // write to num itself plus 2 alert(num); // shows 3

Without running the code, determine what will be displayed on the screen:

let num = 1; num = num + 1; num = num + 1; alert(num);

Without running the code, determine what will be displayed on the screen:

let num = 1; num = num + 2; num = num + 3; alert(num);

Shorthand operations

For such operations, when to the variable its current value plus something else is written, there is a special shortened syntax. This syntax uses the special operator += instead of the normal assignment. Let's look at an example:

let num = 1; num += 3; // equivalent to num = num + 3;

There are similar operators for other mathematical operations:

let num = 2; num -= 3; // equivalent to num = num - 3;
let num = 2; num *= 3; // equivalent to num = num * 3;
let num = 2; num /= 3; // equivalent to num = num / 3;

Modify this code to use the above described shorthand operations:

let num = 47; num = num + 7; num = num - 18; num = num * 10; num = num / 15; alert(num);
enru