Movatterモバイル変換


[0]ホーム

URL:


  1. 개발자를 위한 웹 기술
  2. JavaScript
  3. JavaScript 참고서
  4. 식 및 연산자
  5. 더하기 (+)

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

View in EnglishAlways 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월⁩.

더하기 연산자(+)는 숫자 피연산자의 합을 구하거나, 문자열을 서로 연결합니다.

시도해 보기

console.log(2 + 2);// Expected output: 4console.log(2 + true);// Expected output: 3console.log("hello " + "everyone");// Expected output: "hello everyone"console.log(2001 + ": A Space Odyssey");// Expected output: "2001: A Space Odyssey"

구문

js
x + y

설명

+ 연산자는 숫자 덧셈과 문자열 연결 두 가지 작업을 수행합니다. 계산 시에 먼저두 개의 피연산자를 원시 타입으로 변환합니다. 그 후에, 두 피연산자의 타입을 확인합니다.

  • 만약 한쪽의 피연산자가 문자열인 경우, 나머지 한쪽의 피연산자도문자열로 변환시킨 뒤 연결합니다.
  • 만약 두 피연산자가 모두BigInt인 경우, BigInt 덧셈을 수행합니다. 만약 한쪽의 피연산자가 BigInt이지만 나머지 한쪽의 피연산자가 BigInt가 아닌 경우,TypeError가 발생합니다.
  • 그 외의 경우, 두 피연산자를 모두숫자로 변환시키고, 숫자 덧셈이 수행됩니다.

종종템플릿 리터럴이나String.prototype.concat()이 문자열 연결과 동일하다고 생각하지만, 그렇지 않습니다. 더하기 연산자는 먼저valueOf()를 호출하여 표현식을 원시 타입으로 변환합니다. 반면에, 템플릿 리터럴과concat()은 먼저toString()을 호출합니다. 만약 표현식에@@toPrimitive 메서드가 있는 경우, 문자열 연결은"default"를 힌트로 사용하고, 템플릿 리터럴은"string"을 사용합니다. 이 차이는 문자열과 원시 표현이 다른 객체에 중요합니다. 예를 들어valueOf() 메서드가 에러를 내는Temporal 같은 객체가 있습니다.

js
const t = Temporal.Now.instant();"" + t; // Throws TypeError`${t}`; // '2022-07-31T04:48:56.113918308Z'"".concat(t); // '2022-07-31T04:48:56.113918308Z'

문자열 변환을 위해"" + x를 사용하는 것은 좋지 않습니다.

예제

Number 덧셈

js
// Number + Number -> 덧셈1 + 2; // 3// Boolean + Number -> 덧셈true + 1; // 2// Boolean + Boolean -> 덧셈false + false; // 0

BigInt 덧셈

js
// BigInt + BigInt -> 덧셈1n + 2n; // 3n// BigInt + Number -> TypeError 발생1n + 2; // TypeError: Cannot mix BigInt and other types, use explicit conversions// BigInt와  non-BigInt를 더하기 위해선, 피연산자 중 하나를 변환하십시오.1n + BigInt(2); // 3nNumber(1n) + 2; // 3

문자열 연결

js
// String + String -> 연결"foo" + "bar"; // "foobar"// Number + String -> 연결5 + "foo"; // "5foo"// String + Boolean -> 연결"foo" + false; // "foofalse"

명세

Specification
ECMAScript® 2026 Language Specification
# sec-addition-operator-plus

브라우저 호환성

같이 보기

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp