We can use ++
to increment
the value of a number by 1
.
In the example given above,
we declare the variable number
using the let
keyword
because the value of number
will be changed
when we perform number++
.
We can use --
to decrement
the value of a number by 1
.
Pre-Increment vs Post-Increment
We can also use the increment (++
) operator in two forms: pre-increment and post-increment.
- Pre-increment: The pre-increment operator (
++
) increases the value of a variable by 1 before using its value. Here's an example:
- Post-increment: The post-increment operator (
++
) increases the value of a variable by 1 after using its value. Here's an example:
In both cases, the variable num
is incremented by 1. However, the pre-increment operator ++
increases the value before it is used, while the post-increment operator ++
increases the value after it is used. The same concept applies to the pre- and post-decrement operator (--
), where the value is decreased either before or after it is used.