Thanks to visit codestin.com
Credit goes to developer.mozilla.org

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

증가 연산자(++)

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015년 7월.

증가(++) 연산자 는 피연산자를 증가(1을 더함)시키고 연산자의 위치에 따라 증가하기 전이나 후의 값을 반환합니다.

시도해 보기

let x = 3;
const y = x++;

console.log(`x:${x}, y:${y}`);
// Expected output: "x:4, y:3"

let a = 3;
const b = ++a;

console.log(`a:${a}, b:${b}`);
// Expected output: "a:4, b:4"

구문

js
x++
++x

설명

만약 피연산자 뒤에 연산자를 붙여서 사용한다면 (예를 들어 x++) 증가 연산자는 수를 증가시키고 증가하기 전 값을 반환합니다.

만약 피연산자 앞에 연산자를 붙여서 사용한다면(예를 들어 ++x) 증가 연산자는 수를 증가시키고 증가 후 값을 반환합니다.

증가 연산자는 참조인 피연산자 변수 및 객체 속성, 다시말해 유효한 할당 대상에만 적용할 수 있습니다. ++x 자체는 참조가 아닌 값으로 평가되기 때문에 여러 증가 연산자를 연속적으로 사용할 수 없습니다.

js
++(++x); // SyntaxError: Invalid left-hand side expression in prefix operation

예제

후위 증가

js
let x = 3;
const y = x++;

// x = 4
// y = 3

전위 증가

js
let x = 3;
const y = ++x;

// x = 4
// y = 4

명세

Specification
ECMAScript® 2027 Language Specification
# sec-postfix-increment-operator

브라우저 호환성

같이 보기