← 返回首页
단항 더하기 (+) - JavaScript | MDN

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월.

단항 더하기 연산자(+)는 피연산자 앞에 위치하며 피연산자를 평가하지만, 만약 피연산자가 숫자가 아니라면 숫자로 변환을 시도합니다.

In this article

시도해 보기

const x = 1; const y = -1; console.log(+x); // Expected output: 1 console.log(+y); // Expected output: -1 console.log(+""); // Expected output: 0 console.log(+true); // Expected output: 1 console.log(+false); // Expected output: 0 console.log(+"hello"); // Expected output: NaN

구문

js
+x;

설명

단항 부정 (-)도 숫자가 아닌 값을 변환할 수 있지만, 단항 더하기는 어떤 것을 숫자로 변환하는 가장 빠르고 선호하는 방법입니다. 왜냐하면 숫자에 대해 다른 연산을 수행하지 않기 때문입니다. 정수와 실수의 문자열 표현뿐 아니라 문자열이 아닌 값인 true, false 및 null 또한 변환할 수 있습니다. 10진수 및 16진수(0x 접두사) 형식의 정수 모두 지원됩니다. 음수도 지원됩니다(16진수 제외). BigInt 값에 연산자를 사용하면 TypeError가 발생합니다. 특정 값을 구문 분석할 수 없으면 NaN으로 평가됩니다.

예제

숫자에 사용하기

js
const x = 1; const y = -1; console.log(+x); // 1 console.log(+y); // -1

숫자가 아닌 값에 사용하기

js
+true; // 1 +false; // 0 +null; // 0 +function (val) { return val; }; // NaN +1n; // TypeError 발생: BigInt 값을 숫자로 변경할 수 없습니다

명세

Specification
ECMAScript® 2027 Language Specification
# sec-unary-plus-operator

브라우저 호환성

Enable JavaScript to view this browser compatibility table.

같이 보기