diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
commit | da78a9e329e272dedb2400b79a3bdeebff387d47 (patch) | |
tree | e6ef8aa7c43556f55ddfe031a01cf0a8fa271bfe /files/ko/web/javascript/reference/operators/arithmetic_operators | |
parent | 1109132f09d75da9a28b649c7677bb6ce07c40c0 (diff) | |
download | translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.gz translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.bz2 translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.zip |
initial commit
Diffstat (limited to 'files/ko/web/javascript/reference/operators/arithmetic_operators')
-rw-r--r-- | files/ko/web/javascript/reference/operators/arithmetic_operators/index.html | 290 |
1 files changed, 290 insertions, 0 deletions
diff --git a/files/ko/web/javascript/reference/operators/arithmetic_operators/index.html b/files/ko/web/javascript/reference/operators/arithmetic_operators/index.html new file mode 100644 index 0000000000..8b2b274aa6 --- /dev/null +++ b/files/ko/web/javascript/reference/operators/arithmetic_operators/index.html @@ -0,0 +1,290 @@ +--- +title: 산술 연산자 +slug: Web/JavaScript/Reference/Operators/Arithmetic_Operators +tags: + - JavaScript + - Operator + - Reference +translation_of: Web/JavaScript/Reference/Operators +--- +<div>{{jsSidebar("Operators")}}</div> + +<p><span class="seoSummary"><strong>산술 연산자는</strong> 숫자 값(리터럴 또는 변수)을 피연산자로 받아 하나의 숫자 값을 반환합니다.</span> 표준 산술 연산자는 덧셈(+), 뺄셈(-), 곱셈(*), 나눗셈(/)입니다.</p> + +<div>{{EmbedInteractiveExample("pages/js/expressions-arithmetic.html")}}</div> + + + +<h2 id="덧셈"><a id="Addition" name="Addition">덧셈 (+)</a></h2> + +<p>덧셈 연산자는 숫자 피연산자를 더한 값, 또는 문자열을 연결한 값을 생성합니다.</p> + +<h3 id="구문">구문</h3> + +<pre class="syntaxbox"><strong>연산자:</strong> x + y +</pre> + +<h3 id="예제">예제</h3> + +<pre class="brush: js">// Number + Number -> 합 +1 + 2 // 3 + +// Boolean + Number -> 합 +true + 1 // 2 + +// Boolean + Boolean -> 합 +false + false // 0 + +// Number + String -> 연결 +5 + "foo" // "5foo" + +// String + Boolean -> 연결 +"foo" + false // "foofalse" + +// String + String -> 연결 +"foo" + "bar" // "foobar" +</pre> + +<h2 id="뺄셈_-"><a id="Subtraction" name="Subtraction">뺄셈 (-)</a></h2> + +<p>뺄셈 연산자는 두 개의 피연산자를 뺀 값을 생성합니다.</p> + +<h3 id="구문_2">구문</h3> + +<pre class="syntaxbox"><strong>연산자:</strong> x - y +</pre> + +<h3 id="예제_2">예제</h3> + +<pre class="brush: js">5 - 3 // 2 +3 - 5 // -2 +"foo" - 3 // NaN</pre> + +<h2 id="나눗셈"><a id="Division" name="Division">나눗셈 (/)</a></h2> + +<p>나눗셈 연산자는 왼쪽 피연산자를 피제수로, 오른쪽 피연산자를 제수로 한 몫을 생성합니다.</p> + +<h3 id="구문_3">구문</h3> + +<pre class="syntaxbox"><strong>연산자:</strong> x / y +</pre> + +<h3 id="예제_3">예제</h3> + +<pre class="brush: js">1 / 2 // JavaScript에선 0.5 +1 / 2 // Java에선 0 +// (양쪽 피연산자 모두 명시적인 부동소수점 숫자가 아님) + +1.0 / 2.0 // JavaScript와 Java 둘 다 0.5 + +2.0 / 0 // JavaScript에서 Infinity +2.0 / 0.0 // 동일하게 Infinity +2.0 / -0.0 // JavaScript에서 -Infinity</pre> + +<h2 id="곱셈_*"><a id="Multiplication" name="Multiplication">곱셈 (*)</a></h2> + +<p>곱셈 연산자는 두 연산자의 곱을 생성합니다.</p> + +<h3 id="구문_4">구문</h3> + +<pre class="syntaxbox"><strong>연산자:</strong> x * y +</pre> + +<h3 id="예제_4">예제</h3> + +<pre class="brush: js">2 * 2 // 4 +-2 * 2 // -4 +Infinity * 0 // NaN +Infinity * Infinity // Infinity +"foo" * 2 // NaN +</pre> + +<h2 id="나머지"><a id="Remainder" name="Remainder">나머지 (%)</a></h2> + +<p>나머지 연산자는 왼쪽 피연산자를 오른쪽 피연산자로 나눴을 때의 나머지를 반환합니다. 결과는 항상 피제수의 부호를 따라갑니다.</p> + +<h3 id="구문_5">구문</h3> + +<pre class="syntaxbox"><strong>연산자:</strong> var1 % var2 +</pre> + +<h3 id="예제_5">예제</h3> + +<pre class="brush: js">12 % 5 // 2 +-1 % 2 // -1 +NaN % 2 // NaN +1 % 2 // 1 +2 % 3 // 2 +-4 % 2 // -0 +5.5 % 2 // 1.5 +</pre> + +<h2 id="거듭제곱_**"><a name="Exponentiation">거듭제곱 (**)</a></h2> + +<p>거듭제곱 연산자는 첫 번째 피연산자를 밑으로, 두 번째 피연산자를 지수로 한 값을 생성합니다. 즉, <code>var1</code>과 <code>var2</code>가 변수일 때, <code>var1<sup>var2</sup></code>의 값을 생성합니다. 거듭제곱 연산자는 우결합성을 가집니다. 따라서 <code>a ** b ** c</code>는 <code>a ** (b ** c)</code>와 같습니다.</p> + +<h3 id="구문_6">구문</h3> + +<pre class="syntaxbox"><strong>연산자:</strong> var1 ** var2 +</pre> + +<h3 id="참고">참고</h3> + +<p>PHP와 Python 등 거듭제곱 연산자를 가진 대부분의 언어는 거듭제곱 연산자의 우선순위가 +와 - 등 단항 연산자보다 높습니다. 그러나 Bash는 단항 연산자가 거듭제곱 연산자보다 우선순위가 높은 등 일부 예외도 있습니다. JavaScript는 단항 연산자(<code>+/-/~/!/delete/void/typeof</code>)를 왼쪽 피연산자 앞에 배치할 수 없으므로, 모호한 표현도 작성할 수 없습니다.</p> + +<pre class="brush: js">-2 ** 2; +// Bash에서 4, 다른 언어에서는 -4. +// 모호한 표현이므로 JavaScript에서는 유효하지 않음 + + +-(2 ** 2); +// JavaScript에서 -4, 작성자의 의도가 명확함 +</pre> + +<h3 id="예제_6">예제</h3> + +<pre class="brush: js">2 ** 3 // 8 +3 ** 2 // 9 +3 ** 2.5 // 15.588457268119896 +10 ** -1 // 0.1 +NaN ** 2 // NaN + +2 ** 3 ** 2 // 512 +2 ** (3 ** 2) // 512 +(2 ** 3) ** 2 // 64 +</pre> + +<p>결과의 부호를 뒤집으려면 다음과 같이 작성합니다.</p> + +<pre class="brush: js">-(2 ** 2) // -4 +</pre> + +<p>거듭제곱의 밑을 음수로 강제하려면 다음과 같이 작성합니다.</p> + +<pre class="brush: js">(-2) ** 2 // 4 +</pre> + +<div class="note"> +<p><strong>참고:</strong> JavaScript는 <a href="/ko/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_XOR">비트 연산자 ^ (논리 XOR)</a>도 가지고 있습니다. <code>**</code>와 <code>^</code>는 다릅니다. (예 : <code>2 ** 3 === 8</code>이지만 <code>2 ^ 3 === 1</code>)</p> +</div> + +<h2 id="증가"><a id="Increment" name="Increment">증가 (++)</a></h2> + +<p>증가 연산자는 피연산자를 증가(1을 덧셈)시키고, 그 값을 반환합니다.</p> + +<ul> + <li>피연산자 뒤에 붙여(예: <code>x++</code>) 접미사로 사용한 경우 증가하기 전의 값을 반환합니다.</li> + <li>피연산자 앞에 붙여(예: <code>++x</code>) 접두사로 사용한 경우 증가한 후의 값을 반환합니다.</li> +</ul> + +<h3 id="구문_7">구문</h3> + +<pre class="syntaxbox"><strong>연산자:</strong> x++ or ++x +</pre> + +<h3 id="예제_7">예제</h3> + +<pre class="brush: js">// 접미사 +var x = 3; +y = x++; // y = 3, x = 4 + +// 접두사 +var a = 2; +b = ++a; // a = 3, b = 3 +</pre> + +<h2 id="감소_--"><a id="Decrement" name="Decrement">감소 (--)</a></h2> + +<p>감소 연산자는 피연산자를 감소(1을 뺄셈)시키고, 그 값을 반환합니다.</p> + +<ul> + <li>피연산자 뒤에 붙여(예: <code>x--</code>) 접미사로 사용한 경우 감소하기 전의 값을 반환합니다.</li> + <li>피연산자 앞에 붙여(예: <code>--x</code>) 접두사로 사용한 경우 감소한 후의 값을 반환합니다.</li> +</ul> + +<h3 id="구문_8">구문</h3> + +<pre class="syntaxbox"><strong>연산자:</strong> x-- or --x +</pre> + +<h3 id="예제_8">예제</h3> + +<pre class="brush: js">// 접미사 +var x = 3; +y = x--; // y = 3, x = 2 + +// 접두사 +var a = 2; +b = --a; // a = 1, b = 1 +</pre> + +<h2 id="단항_부정_-"><a name="Unary_negation">단항 부정 (-)</a></h2> + +<p>단항 부정 연산자는 피연산자의 앞에 위치하며 부호를 뒤집습니다.</p> + +<h3 id="구문_9">구문</h3> + +<pre class="syntaxbox"><strong>연산자:</strong> -x +</pre> + +<h3 id="예제_9">예제</h3> + +<pre class="brush: js">var x = 3; +y = -x; // y = -3, x = 3 + +// 단항 부정 연산자는 숫자가 아닌 값을 숫자로 변환함 +var x = "4"; +y = -x; // y = -4</pre> + +<h2 id="단항_양부호"><a name="Unary_plus">단항 양부호 (+)</a></h2> + +<p>단항 양부호 연산자는 피연산자의 앞에 위치하며 피연산자의 값 그대로 평가되지만, 값이 숫자가 아닐 경우 숫자로 변환을 시도합니다. 단항 부정(-) 연산자도 숫자가 아닌 값을 변환할 수 있지만, 단항 양부호가 속도도 제일 빠르고 다른 연산도 수행하지 않으므로 선호해야 할 방법입니다. 문자열로 표현한 정수 및 부동소수점 실수, 문자열이 아닌 <code>true</code>, <code>false</code>, <code>null</code>도 변환할 수 있습니다. 10진수와 (앞에 "0x"가 붙은) 16진수 정수 모두 지원합니다. 음수도 지원하지만 16진수 음수에는 사용할 수 없습니다. 어떤 값을 분석할 수 없으면 {{jsxref("NaN")}}으로 평가됩니다.</p> + +<h3 id="구문_10">구문</h3> + +<pre class="syntaxbox"><strong>연산자:</strong> +x +</pre> + +<h3 id="예제_10">예제</h3> + +<pre class="brush: js">+3 // 3 ++"3" // 3 ++true // 1 ++false // 0 ++null // 0 ++function(val) { return val } // NaN +</pre> + +<h2 id="명세">명세</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-additive-operators', 'Additive operators')}}</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-postfix-expressions', 'Postfix expressions')}}</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-11.5', 'Multiplicative operators')}}</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-11.4', 'Unary operator')}}</td> + </tr> + </tbody> +</table> + +<h2 id="브라우저_호환성">브라우저 호환성</h2> + + + +<p>{{Compat("javascript.operators.arithmetic")}}</p> + +<h2 id="같이_보기">같이 보기</h2> + +<ul> + <li><a href="/ko/docs/Web/JavaScript/Reference/Operators/Assignment_Operators">할당 연산자</a></li> +</ul> |