++
는 특정 변수에 1을 바로 더해준다.
다만, ++
가 변수 이름 앞에 오는 것과 뒤에 오는 것에 차이가 있다.
let a = 1;
console.log(a++); // 1 출력됨
console.log(a); // 2 출력됨
console.log(a++);
를 할 때에는 1 더하기 직전 값을 보여준다.
그리고 나서 a
를 출력했을 때, 1 더해진 2가 출력된다.
let a = 1;
console.log(++a); // 2 출력됨
console.log(a); // 2 출력됨
console.log(++a);
를 할 때에는 a
가 이미 1 더해진 상태이기 때문에 2가 출력된다.
그리고 나서 a
를 출력했을 때, 역시 2가 출력된다.