Increment in JavaScript - ECMAScript 2021

Find this useful? Support us: Star on GitHub 6
Category: Number | Language: JavaScript - ECMAScript 2021

To increment a number in JavaScript - ECMAScript 2021, you can use the unary ++ operator, which increases the value of the variable by 1. It can be used in both prefix and postfix forms.

Here are a few examples:

let num = 5;
num++; // postfix increment operator
console.log(num); // Output: 6

let x = 10;
++x; // prefix increment operator
console.log(x); // Output: 11

In the above example, the first use of ++ is postfix, so the value of num is incremented after it is returned and the output value is 6. In the second example, the ++ operator is prefix, which means the value of x is incremented before the value is returned to the console and the output value is 11.