# 연산자

## 산술 연산자

산술 연산자에는 더하기(+), 빼기(-), 곱하기(\*), 나누기(/), 나머지(%)가 있습니다. \
산술연산자로 연산을 하기 위해서는 연산 대상 데이터가 반드시 2개이상 있어야 합니다.

| 종류 | 기본형  | 설명  |
| -- | ---- | --- |
| +  | A+B  | 더하기 |
| -  | A-B  | 빼기  |
| \* | A\*B | 곱하기 |
| /  | A/B  | 나누기 |
| %  | A%B  | 나머지 |

```javascript
<script>
    var num1 = 15;
    var num2 = 100;
    var result = 400;
    
    result = num1 + num2; //결괏값 115
    document.write(result,"<br />"); 
    
    result = num1 - num2; //-85
    document.write(result,"<br />");
    
    result = num1 * num2; //1500
    document.write(result,"<br />");
    
    result = num1 / num2; //0.15
    document.write(result,"<br />");
    
    result = num1 % num2; //15
    document.write(result,"<br />");
</script>
```

## 대입 연산자

대입 연산자(=)는 연산된 데이터를 변수에 저장할 때 사용합니다.\
복합 대입 연산자(+=, -=, \*=, -=, %=)는 산술 연산자와 대입 연산자가 복합적으로 적용된 것을 말합니다.

| 종류      | 풀이         |
| ------- | ---------- |
| A = B   | A = B      |
| A += B  | A = A + B  |
| A \*= B | A = A \* B |
| A /= B  | A = A / B  |
| A %= B  | A = A % B  |

```javascript
<script>
    var num1 = 100;
    var num2 = 200;
    
    //num1 = num1 + num2;
    num1 += num2;
    document.write(num1,"<br>"); //300
    
    //num1 = num1 - num2;
    num1 -= num2;
    document.write(num1,"<br>"); //100
    
    //num1 = num1 * num2;
    num1 *= num2;
    document.write(num1,"<br>"); //20000
    
    //num1 = num1 % num2;
    num1 %= num2;
    document.write(num1,"<br>"); //0
</script>
```

```javascript
    <script>
        var str ="<table>";
        str += "<tr>"
            str += "<td>총시간 (%)</td>";
            str += "<td>NCS 소양교과(%)</td>";
            str += "<td>NCS 전공교과 (%)</td>";
            str += "<td>비 NCS 교과 (%)</td>";
        str += "</tr>"

        str += "<tr>"
        str += "<td>950(100%)</td>";
        str += "<td>34 (3.6%)</td>";
        str += "<td>916 (96.4%)</td>";
        str += "<td>-(-%)</td>";
        str += "</tr>"
        str +="</table>";
        document.write(str);
        
//        var str = "<table>";
//        str += "<tr>"; tr은 한 
//        str += "<td>1</td>";
//        str += "<td>2</td>";
//        str += "</tr>";
//        str += "</table>";
//        document.write(str);
        
    </script>
```

## 증감 연산자&#x20;

증감 연산자에는 숫자형 데이터를 1씩 증가시키는 증가 연산자 (++)와 반대로 1씩 감소시키는 감소 연산자(--)가 있습니다.\
증감 연산자는 앞 연산자들과 달리 피연산자가 한개만 필요한 단항 연산자 입니다.\
증감 연산자는 변수의 어느 위치에 오는가에 따라 결괏값이 달라집니다.

{% hint style="info" %}
기본형:\
1\. 변수의 값을 1만큼 감소시킵니다.\
&#x20;   변수--; 또는 --변수;\
2\. 변수의 값을 1만큼 증가시킵니다.\
&#x20;   변수++; 또는 ++변수;
{% endhint %}

> 변수 = ++A ; 일때  **A의 값이 1만큼 증가**되고 증가된 **A의 값이 변수에 대입**됩니다.\
> 변수 = A++; 일때 **A의 값이 변수에 대입**되고 **A의 값을 1만큼 증가**됩니다.

```javascript
let num1 = 10; //전역변수 num1에 숫자 10을 할당
let num2 = 20; //전역변수 num2에 숫자 20을 할당
let num3 = 20; //전역변수 num3에 숫자 20을 할당
let rusult; //선언
document.write(num1, "<br>");//10

num1--;
document.write(num1, "<br>");//9

num1++;
document.write(num1, "<br>");//10

rusult = num2++;
document.write(rusult, "<br>");//20
document.write(num2, "<br>");//21

rusult = ++num3;
document.write(rusult, "<br>");//21
document.write(num3, "<br>");//21
```

```javascript
function func1(){
    let result, a = 100, b = 200, c = 300;
    result = a < b ? b++ : --c;
    document.write(result,"<br>");
    document.write(b);
}
func1();
//200
//201

function func2(){
    let hap, j, k, l;
    j = k = l = 0;
    hap = ++j + k++ + ++l;
    document.write(hap);
    document.write(j);
    document.write(k);
    document.write(l);
}
func2();
//2111
```

## 비교 연산자

두 데이터를 '크다, 작다, 같다'와 같이 비교할 때 사용하는 연산자 입니다.\
연산된 결괏값은 true(참) 또는 false(거짓)로 논리형 데이터를 반환합니다.

| 종류  | 예시      | 설명                                    |
| --- | ------- | ------------------------------------- |
| ==  | x == y  | 좌변과 우변이 같다.                           |
| === | x === y | 좌변과 우변이 같다. 데이터형(문자형, 데이터형, 논리형)도 같다. |
| !=  | x != y  | 좌변과 우변이 다르다.                          |
| !== | x !== y | 좌변과 우변이 다르다. 데이터 형도 다르다.              |
| >   | x > y   | 좌변이 우변보다 크다.                          |
| <   | x < y   | 좌변이 우변보다 작다.                          |
| >=  | x >= y  | 좌변이 우변보다 크거나 같다.                      |
| <=  | x <= y  | 좌변이 우변보다 작거나 같다.                      |

```javascript
let a = 10;
let b = 20;
let c = 10;
let f = "20";
let result;

result = a > b; //10 이 20보다 크다.
document.write(result, "<br>");// false

result = a < b; //10이 20보다 작다.
document.write(result, "<br>");//true

result = a <= b; //10이 20보다 작거나 같다.
document.write(result, "<br>");//true

result = b == f; // 20 과 20은 같다.
document.write(result, "<br>");//true

result = a != b; // 10과 20은 다르다.
document.write(result, "<br>");//true

result = b === f; //20과 20은 같다. 데이터 형도 같다.
document.write(result, "<br>");// false
```

```javascript
function func3(){
    let i=10, j=10, k=30;
    i /= j; // i = i / j  // i = 1
    j -= i; // j = j - i  // j - i(1) = 9
    k %= j; // k = k % j  // k % j(3) = 3
    
    document.write(i);
    document.write(j);
    document.write(k);
}
func3();
//193
```

## 논리연산자

논리 연산자에는 ∥(or), &&(and), !(not)이 있으며, 논리 연산자는 피연산자가 논리형 데이터인 true 또는 false로 결괏값을 반환합니다.∥(or) 연산자는 피연산자 중 하나만 true이면 true라는 결괏값을 반환합니다. 하지만  &&(and) 연산자는 피연산자 중 하나만 false이면 false라는 결괏값을 반환합니다. !(not)은 논리 부정 연산자로, 피연산자가 true이면 false라는 반대의 결괏값을 반환합니다.

| 종류   | 예시       | 설명                                |
| ---- | -------- | --------------------------------- |
| &&   | X && Y   | 둘 다 true인 경우 true를 반환합니다.         |
| \|\| | X \|\| Y | 둘 중의 하나 이상이 true인 경우 true를 반환합니다. |
| !    | !X       | 반대 값을 반환합니다.                      |

```javascript
let a = 10;
let b = 20;
let m = 30;
let n = 40;
let result;

result = a>b || b>=m || m>n; //false || false ||false
document.write(result, "<br>"); //false

result = a>b || b>=m || m<=n; //false || false || true
document.write(result, "<br>"); //true

result = a<=b && b>=m && m<=n; //true && false && true
document.write(result, "<br>"); //false

result = a<=b && b<=m && m<=n; //true && true && true
document.write(result, "<br>"); //true

result =!(a>b); //!false를 계산
document.write(result, "<br>"); //true
```

```javascript
document. write (false || false); 
document. write ("<br>");
document. write (false || true);
document. write ("<br>");
document. write (true || true);

document. write (false && false);
document. write ("<br>");
document. write (false && true);
document. write ("<br>");
document. write (true & &true);
//false
//true
//true false
//false
//true
```

## 연산자 우선순위

연산자들의 우선순위는 다음과 같습니다.

1. ()
2. 단항 연산자(--, ++, !)
3. 산술 연산자(\*, / %, +, -)
4. 비교 연산자(>, >=, <, <=, == ===, !==, !=)
5. 논리연산자(&&, ∥)
6. 대입(복합 대입)연산자(=, +=, -=, \*=, -=, %=)

이러한 우선순위를 고랴하여 ++A\*B<=C라는 코드를 실행한다면 먼저 변수 A에 데이터 1을 증가시키고 B를 곱한 다음 마지막으로 곱한 값을 C와 비교한 후 최종 결괏값을 반환합니다.

#### 정리 연습

```javascript
//        let userHeight = 180;
//        let userWeight = 74;
//        
//        //평균 체중 = (신장-100)*0.9;
//        let normal = (userHeight - 100) * 0.9;
//        document.write(normal);

let name = prompt("당신의 이름은?","");
let height = prompt; ("당신의 신장은?","");
let weight = prompt; ("당신의 몸무게는?","");

let normal = (height - 100) * 0.9; //평균 체중 값 저장
document.write(name,"<br>");
document.write(height,"<br>");
document.write(weight,"<br>");
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jangar6621.gitbook.io/javascript-jquery/undefined-4.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
