Ask coding questions

← Back to all posts
result of console.log
kaitlynnT (1)

This is my code(JS) as below

let x = 3
console.log(x++)

It showed 3 instead of 4.

please help to answer it.
Thanks

Answered by a5rocks (774) [earned 5 cycles]
View Answer
Commentshotnewtop
a5rocks (774)

x++ returns the previous x, ++x returns the new x.

Both increment so this would work:

let x = 3;
x++; // or ++x;
console.log(x);
kaitlynnT (1)

@a5rocks thanks for your help!