연산자

산술연산자

산술연사자에는 산수 기간에 배운 더하기(+), 빼기(-), 곱하기(*), 나누기(/), 나머지(%)가 있습니다. 산술 연산자로 연산을 하기 위해서는 연상 대상 데이터가 반드시 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 />");
</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

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

// 줄인말이라고 생각하면됨
<script>
//        var str = "<table>";
//        str += "<tr>"
//        str += "<td>1</td>"
//        str += "<td>2</td>"
//        str += "</tr>"
//        str += "</table>"; 
//        
//        document.write(str);
    
    var str = "<table>";
    str += "<tr>"
    str += "<th>총시간</th>"
    str += "<th>NCS 소양 교과</th>"
    str += "<th>NCS 전공 교과</th>"
    str += "<th>비 NCS 교과</th>"
    
    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>"

    document.write(str);
    
</script>
function func3(){
    let i = 10, j=10, k=30;
    i /=j; // i = i / j
    j -=i; // j = j - i
    k %=j; // k = k % j
    document.write(i); //1
    document.write(j); //9
    document.write(k); //3
}
func3();

증감연산자

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

let num1 = 10;  //전역변수 num1에 숫자 10을 설정
let num2 = 20;  //전역변수 num2에 숫자 20을 설정
let num3 = 20;  //전역변수 num3에 숫자 20을 설정
let result;

    document.write(num1,"<br>");   //10
    
    num1--;
    document.write(num1,"<br>");   //9
    
    num1++;
    document.write(num1,"<br>");   //10
    
    result = num2++; //++ 뒤일 경우 result 값에 반영 안됨
    document.write(result,"<br>"); //20
    document.write(num2,"<br>");   //21
    
    result = ++num3; //++ 앞일 경우 result 값에 반영
    document.write(result,"<br>"); //21
    document.write(num3,"<br>");   //21

변수의 어느위치에 오는가에 따라 결괏값이 달라집니다.

① 먼저 ⓐ(b의 값을 1만큼 증가)가 실행되고, ⓑ(증가된 B의 값을 A에 대입)가 실행됩니다.

var A = ++B ⓐ = ++B ⓑ = ++B -> A

② 먼저 ⓐ(B의 값을 A에 대입)가 실행되고. ⓑ(B의 값을 1만큼 증가)가 실행됩니다.

var A = B++ ⓐ = A = b ⓑ = B++;

function func1(){
    let result, a = 100, b = 200 , c = 300;
    result = a < b ? ++b : --c;
    document.write(result,"<br>");
}
func1(); // 200
function func2(){
    let hap, j, k, l;
    j = k = l = 0;
    hap = ++j + k++ + ++l;
    document.write(hap); // 2
    document.write(j); // 1 
    document.write(k); // 1
    document.write(l); // 1
}
func2(); 

비교연산자

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

연산자

예시

설명

==

x == y

좌변과 우변이 같다.

===

x === y

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

!=

x != y

좌변과 우변이 다르다.

!==

x !== y

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

>

x > y

좌변이 우변보다 크다.

<

x < y

좌변이 우변보다 작다.

>=

x >= y

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

<=

x <= y

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

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

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

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

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

result = b==f;
document.write(result,"<br>") //true

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

result = b ===f;
document.write(result,"<br>") //false  좌변과 우변이 같으나 데이터형이 다름

논리연산자

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

연산자

예시

설명

&&

X && Y

둘다 true인 경우 true를 반환합니다.

||

X || Y

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

!

!X

반대 값을 반환합니다.

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

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

연산자 우선순위

일반적인 산수를 연산할 때처럼 연산자에도 우선순위가 있습니다. 예를 들어 '2+(1*3)=5' 와 같은 식입니다. 이 식의 결과가 '9' 라고 대답한다면 우선순위를 고려하지 않은 것이겠죠?

Last updated

Was this helpful?