연산자

산술 연산자

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

종류

기본형

설명

+

A+B

더하기

_

A-B

빼기

*

A*B

곱하기

/

A/B

나누기

%

A%B

나머지

<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/>")
    
    //115
    //-85
    //1500
    //0.15
    //15
</script> 
function func4 (){
    let i = 10, j = 10, k = 30;
    i /= j; 
    j -= i;
    k %= j; 
    document.write(i,"<br>");
    document.write(j,"<br>");
    document.write(k)
}

func4();
document.write("<br>")

대입 연산자

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

종류

풀이

A = B

A = B

A += B

A = A+B

A *= B

A = A*B

A /= B

A = A/B

A %= B

A = A%B

표 만들기

var t ="<table>"
t +="<thead>"
t +="<tr>"
t +="<th>총시간%</th>"
t +="<th>NCS 소양 교과(%)</th>"
t +="<th>NCS 전공 교과(%)</th>"
t +="<th>비 NCS 교과-(-%)</th>"
t +="</tr>"
t +="</thead>"
t +="<tbody>"
t +="<tr>"
t +="<td>950(100%)</td>"
t +="<td>34(3.6%)</td>"
t +="<td>916(96.%)</td>"
t +="<td>-(-%)</td>"
t +="</tr>"
t +="</tbody>"
t +="</tabel>"
document.write(t)

문자 결합 연산자

문자 결합 연산자는 피연산자(연산 대상 데이터)가 문자형 데이터입니다. 여러 개의 문자를 하나의 문자형 데이터로 결합할 때 사용합니다.

증감 연산자

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

<script>
    let num1=10; //전역 변수 num1에 숫자 10을 할당
    
    num1--;
    document.write(num1,"<br>");

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

//9
//10
<script>
    let num1=10; //전역 변수 num1에 숫자 10을 할당
    let rusult;    
    
    rusult = num1++;
    document.write(rusult,"<br>")
    document.write(num3,"<br>")
    
    rusult = ++num1;
    document.write(rusult,"<br>")
    document.write(num3,"<br>")
</script>

//10
//11
//11
//11

*증감 연산자를 뒤에 주면 앞에 있는 변수가 먼저 적용되고 그 다음에 증감 연산자가 적용된다.

function fun1(){
    let result, a = 100, b = 200, c = 200;
    result = a < b ? b++ : --c;
    document.write(result);
}
fun1();
document.write("<br>")

//200

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

비교 연산자

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

연산자

예시

설명

==

x == y

좌변과 우변이 같다.

===

x === y

좌변과 우변이 같고 데이터형도 같다.

!=

x != y

좌변과 우변이 다르다.

!==

x !== y

좌변과 우변이 다르고 데이터형도 다르다.

>

x > y

좌변이 우변보다 크다.

<

x < y

좌변이 우변보다 작다.

>=

x >= y

좌변이 우변보다 크거나 같다.

<=

x <= y

좌변이 우변보다 작거나 같다.

<script>
    let a = 10;
    let b = 20;
    let c = 10;
    let f = "20";
    let result;
    
    result = a > b;
    document.write(result,"<br>");
    
    result = a < b;
    document.write(result, "<br>");
    
    result = a <= b;
    document.write(result, "<br>");
    
    result = b == f;
    document.write(result, "<br>");
    
    result = a != b;
    document.write(result, "<br>");
    
    result = b === f;
    document.write(result,"<br>");        
</script>

//false (a가 b보다 작기때문에 false)
//true (a가 b보다 작기때문에 true)
//true (a가 b보다 작고 조건이 작거나 같으면이기 때문에 true)
//true (b와 f의 값(숫자)이 같으므로 true)
//true (a와 b의 값이 다르므로 true)
//false (b와 f의 데이터형이 다르므르 (숫자와 문자) fales)

*'=='는 값을를 비교하지만 '===' 데이터형까지 분석해준다.

논리 연산자

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

연산자

예시

설명

&&

X && Y

둘 다 true인 경우 true를 반환한다.

||

X || Y

둘 중의 하나 이상이 true인 경우 true를 반환한다.

!

! X

반대 값을 반환한다.

<script>
    let result;
    
    document.write(false||false);
    document.write("<br>");
    document.write(false||true);
    document.write("<br>")
    document.write(true||true);
    
    document.write("<br>")
    document.write("<br>")
    
    document.write(false && false);
    document.write("<br>"); 
    document.write(false && true);
    document.write("<br>")
    document.write(true && true);
    
    document.write("<br>")
    document.write("<br>")
    
    document.write(! true);
    document.write("<br>")
    document.write(! false);
</script>

//false
//true
//true

//false
//false
//true

//false
//true

연산자 우선 순위

일반적인 산수를 연산할 때 연산자에도 우선순위가 있습니다. 예를 들면 '2+(1*3)=5'와 같은 식 입니다.

Last updated

Was this helpful?