Member-only story
For more questions and answers visit our website at Frontend Interview Questions
Increment Operator (++)
The increment operator (++
) increases the value of a variable by one. It can be used in two forms:
- Postfix Increment (
i++
): Increments the value after returning the original value. - Prefix Increment (
++i
): Increments the value before returning the new value.
Example:
let x = 5;
let y = x++; // y is assigned the value of x (5), then x is incremented to 6
console.log(x); // Output: 6
console.log(y); // Output: 5
let a = 5;
let b = ++a; // a is incremented to 6, then b is assigned the value of a (6)
console.log(a); // Output: 6
console.log(b); // Output: 6
In this example, x++
returns the original value of x
before incrementing it, whereas ++a
increments a
before returning the new value.
Decrement Operator ( — )
The decrement operator (--
) decreases the value of a variable by one. It also has two forms:
- Postfix Decrement (
i--
): Decrements the value after returning the original value. - Prefix Decrement (
--i
): Decrements the value before returning the new value.