Prefix increment/decrement

The prefix increment and decrement operators allow you to increment or decrement any given value and will evaluate to the newly incremented value:

let n = 0;

++n; // => 1 (the newly incremented value)
n; // => 1 (the newly incremented value)

--n; // => 0 (the newly decremented value)
n; // => 0 (the newly decremented value)

++n would technically be equivalent to the following additive assignment:

n += Number(n);

Note how the current value of n is first converted into Number. This is the nature of both the increment and decrement operators: they operate strictly on numbers. So, if n were Stringthat could not be coerced successfully, then the new incremented or decremented value of n would be NaN:

let n = 'foo';
++n; // => NaN
n; // => NaN

Here, we can observe how, since the coercion of foo to a Number fails, the attempted incrementation of it also fails, returning NaN.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.126.199