Open In App

How to Increment a Variable by 1 in JavaScript ?

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, you can increment a variable by 1 using the increment operator (++). There are two ways to use this operator:

Post-increment

The current value of the variable is used in the expression, and then the variable is incremented.

Example: Using post-increment.

Javascript




let x = 5;
let y = x++;
 
console.log(x); // Outputs 6 (original value incremented)
console.log(y); // Outputs 5 (original value used in the expression)


Output

6
5

Pre-increment

The variable is incremented first, and then the updated value is used in the expression.

Example: Using pre-increment.

Javascript




let a = 10;
let b = ++a;
 
console.log(a); // Outputs 11 (original value incremented)
console.log(b); // Outputs 11 (updated value used in the expression)


Output

11
11

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads