In this article, we’ll look at the use of the
The Unary Operators
-- operators let us increment and numeric variable by 1.
For instance, if we have the following code:
let x = 1; x++;
x is first assigned to one, and then it becomes 2 because
x++ increases it by 1.
++ operator can also be used before a variable. For instance, we can write the following code:
let x = 1; ++x;
The code above also assigns the variable to 1 first. Then it increases the variable
x to 2.
The difference between using it before the variable and using it after is that when it’s added after the variable, then it increments and returns the value before incrementing.
If it’s used before the variable, then it increments and returns the value after incrementing.
That made no difference before or after in our case because we didn’t assign it to any variable.
However, if we’re assigning it to a variable or constant, then it matters. For instance, if we have the following code:
let x = 1; const y = ++x;
y are both 2 since
x ‘s latest value is returned after it’s incremented and then the latest value is assigned to
On the other hand, if we have the following code:
let x = 1; const y = x++;
x is 2 and
y is 1 since
x ‘s old value is returned when the increment operation is being done, so it’s the old value of 1 is assigned to
-- is similar to
++ except that it decrements the variable by 1 rather than incrementing it by 1.
We can see that the similar result as
++ if the
-- is applied before or after the variable.
-- comes before or after the variable matters.
Spaces also matter. We shouldn’t have any spaces whether the operators are added before or after the variable.
For instance, we shouldn’t write code like the following:
let x = 1; let y = 1; x ++ y;
In the code above
x stays as 1 but
let x = 1; let y = 1; x ++y;
y is incremented, but
To make increment and decrement operations clearer, we can instead write the following:
let x = 1; x += 1;
x is increment by 1, so
x becomes 2.
This also works for incrementing
x by any number. So we can write something like:
let x = 1; x += 2;
x by 2.
There’re also corresponding operators for subtraction, multiplication or division.
For instance, we can write the following for subtraction:
let x = 1; x -= 2;
For multiplication, we can write:
let x = 1; x *= 2;
and for division, we can write the following:
let x = 1; x /= 2;
These are clearer that
-- , so we should also consider using these operators to arithmetic operations in addition to assigning the new value to the same variable.
No Tab Characters
Tab characters are a pain. They aren’t consistent in all operating systems or text editors.
Therefore, we should think twice about using tabs in our code. Using tabs saves typing, but creates issues with inconsistencies of their spacing between operating systems and text editors.
One good way to keep using the tab key to save typing is to convert them to 2 spaces.
Spaces are almost always consistent with most text editors and operating systems. Therefore, there won’t be any issues with spacing when we opening the same file in different text editors or operating system utilities.
-- operators may bring confusion. They can be added before or after a variable and they act differently as to when the updated value is returned.
If these operators are applied before the variable, then the newest value is returned right away so that we can assign the updated to another variable.
On the other hand, the value is returned value would still be the old value, so if we try to assign to a variable, it would be assigned the old value.
They both increment, but the returned value for them is different.
Tab characters shouldn’t be used in our code. If we want to save typing, we should convert tabs to spaces automatically.