JavaScript Increment and Decrement Operators

JavaScript offers a wide range of operators to assess both mathematical and logical expressions within your code. Among these, the increment and decrement operators stand out as two types of unary operators that either add or subtract one from a specified operand. This article aims to thoroughly explain the principles and functionalities of these two unary operators.

To put it differently, in JavaScript, the increment and decrement operators yield the value subsequent to adding 1 or subtracting 1 (-1). Throughout this discussion, we will frequently refer to the term "operand." An operand represents the quantity of work that needs to be performed. For instance, in the mathematical expression 3 + 5, both 3 and 5 serve as operands alongside the operator.

The increment and decrement operators are applicable exclusively to integer variables and operators that hold numeric values. They cannot be applied to variables that contain character or string values. These operators can be positioned in either prefix or postfix formats. The placement of the increment or decrement operation within the program statement influences the behavior of that statement.

The role of a postfix operator holds greater significance compared to that of a prefix operator. Postfix operators are evaluated based on left-to-right associativity, whereas prefix operators are assessed using right-to-left associativity.

Syntax:

Let x be the operand.

Increment:

Example

x++ or ++x.

Decrement:

Example

x-- or --x.

The increment (++) and decrement (--) operators can be applied either prior to or subsequent to the variable. Below is an illustration of how this is represented in your code:

Example

// Increment
let a = 1;
a++;
++a;
// Decrement
let b = 1;
b--;
--b;

Increment Operator

The main function of increment operators is to increase the numerical value of a variable by one. In programming languages, the increment operator is represented by the symbol "++".

Syntax:

Pre-Increment Operator:

Example

++ variable_name;

Post-Increment Operator:

Example

variable_name ++;

Example

Below is an illustration wherein the outcomes are yielded when the operator (prefix increment) is utilized prior to the operand.

Example

let a = 1;
console.log(++a); //using pre-increment operator result
console.log(a);

Output

In this scenario, the value is first raised and subsequently lowered.

Example

In this section, we will examine the outcome produced by utilizing the increment operator (specifically the postfix increment) subsequent to the operand.

Example

let a = 1;
console.log(a++);//using post-incerement operator result
console.log(a);

Output

The value is increased only after it has been returned for the first time.

Example

let a = 4;
console.log(++a); // =>5
console.log(a);   // =>5
console.log(a++); // =>5
console.log(a);   // =>6

Output

The value of ++x is displayed on the screen. Given that this represents a prefix increment, the value is first incremented by 1 before the resulting value is returned and printed. As a result of this modification, executing console.log(a) yields the output of 5.

Continue using x++, as this operation is a postfix increment, which means it yields the value prior to its increase by 1. The command console.log(a) resulted in the output of 6 because of the modification in the value.

Decrement Operator

The main function of the Decrement Operator is to decrease the numerical value of a variable by 1. In various programming languages, the representation for the Decrement Operator is "- -".

In JavaScript, the decrement operator serves to decrease the value of a variable by one. The position of the decrement operator—either preceding the operand (prefix decrement) or following it (postfix decrement)—affects the value that is returned. When the operator is placed before the operand, the variable's value is diminished by one and then returned. Conversely, if the operator is placed after the operand, the current value is returned first, followed by the decrement.

The decrement operator is limited to the attributes of variables and objects, which means it cannot be utilized with other kinds of references. Additionally, it is not feasible to chain the decrement operator.

Syntax:

Pre-Decrement Operator:

Example

--variable_name ;

Post-Decrement Operator:

Example

variable_name -- ;

Example

In this section, we will examine the outcome produced by utilizing the decrement operator (postfix decrement) following the operand.

Example

let b = 1; 
console.log(b--); 
console.log(b);

Output

It is clear that the value is initially returned before it is subsequently diminished.

Example

In this section, we will examine the results produced when the operator (prefix decrement) is utilized prior to the operand.

Example

let b = 1; 
console.log(--b); 
console.log(b);

Output

In this scenario, the result is yielded subsequent to a reduction in value.

Conclusion

  • There seems to be no difference between using prefixes and suffixes. This is where the problem arises.
  • Unlike other operators that have no side effects and easily return a new value, increment and decrement do not only return a value but also perform a change.
  • When using prefix, the variable is modified first and then returned.
  • When using postfix, the variable is modified first and then the value is altered.

Input Required

This code uses input(). Please provide values below: