How to Increment a Variable by 1 in JavaScript ?
Last Updated :
29 Jan, 2024
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);
console.log(y);
|
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);
console.log(b);
|
Share your thoughts in the comments
Please Login to comment...