상세 컨텐츠

본문 제목

연산자 (Arithmetic, Assignment, String, Comparison, Logical)

FrontEnd/JavaScript

by H_Develop 2022. 6. 28. 17:55

본문

종류

산술(arithmetic) 연산자, 할당(assignment) 연산자, 문자열(String) 연산자, 비교(comparison) 연산자, 논리(logical) 연산자

 

산술 연산자 Arithmetic

    <script>
        var x=15; y=4;

        var a = x + y; b = x - y; c = x * y; d = x / y; e = x % y; f = x **3;
        var g = x++; h = --y; i = (10+x)*y;
        
        document.write(x);
        document.write("<br>");
        document.write(y);
        document.write("<br>");
        document.write(a);
        document.write("<br>");
        document.write(b);
        document.write("<br>");
        document.write(c);
        document.write("<br>");
        document.write(d);
        document.write("<br>");
        document.write(e);
        document.write("<br>");
        document.write(f);
        document.write("<br>");
        document.write(g);
        document.write("<br>");
        document.write(h);
        document.write("<br>");
        document.write(i);
    </script>
    
    값
16
3
19
11
60
3.75
3
3375
15
3
78

 

 

할당 연산자 assignment

 

데이터나 변수 값을 다시 변수에 저장해서 메모리 공간에 할당
처리속도가 빨라진다.

    <script>
        var x = 10;
        x += 2;
        document.write(x);
        document.write("<br>");
        var x = 10;
        x -= 2;
        document.write(x);
        document.write("<br>");
        var x = 10;
        x *= 2;
        document.write(x);
        document.write("<br>");
        var x = 10;
        x /= 2;
        document.write(x);
        document.write("<br>");
        var x = 10;
        x %= 2;
        document.write(x);
        document.write("<br>");
    </script>
    
값
12
8
20
5
0

비교 연산자 comparison

두개의 데이터(Or 변수)의 값을 서로 비교하는데 사용된다.

 

"3" == 3; true 데이터 타입이 달라도 true
3 === 3; true 3은 3과 같다. 데이터타입도 같다. true
"3"===3; false 데이터 타입이 달라서 false
3 != 3; false 3과 3은 같으므로 false
"3" != 3; false "3"과 3을 같다고 해서 false
5 > 3 true 5가 3보다 크므로 true
5 < 3 false 5가 3보다 크므로 false
5 >= 5 true 5는 5보다 작거나 같으므로 true
5 <= 5 true 5는 5보다 크거나 같으므로 true

 

불일치 연산자 (!==)

자료형 변환 없이 두 피연산자가 엄격히 같은지 판별
두 피연산자가 같지 않거나, 같은 자료형이 아닐 때 true를 반환

 

    <script>
        var a = 3; 
        var b = "3"; 
        var c = 5; 
        var d = 3;
        document.write((a == b) + "<br>");
        document.write((a === b) + "<br>");
        document.write((a != b) + "<br>");
        document.write((a !== b) + "<br>");
        document.write((a > c) + "<br>");
        document.write((a < c) + "<br>");
        document.write((a >= c) + "<br>");
        document.write((a <= d) + "<br>");
    </script>
    
true
false
false
!== : true
false
true
false
true

 

논리 연산자 logical

논리연산을 통해 상황을 판단하고 명령을 수행

 

&&(and)     ||(or)       !(NOT) 
empersand vertical bar  caret ^ tild ~ esterlist * open ( blocket )
open / close braket [ ] 
open / close set { }

&&(and) 두 조건이 모두 true 일때 true 반환
||(or)       두 조건 중 하나만 true 여도 true 반환
!(NOT)    true 일 때 false를, false 일 때 true를 반환

 

    <script>
        var x =10;
        var y =60;

        document.write( (x>30 && y>50) + "<br>");
        document.write( (x>30 || y>50) + "<br>");
        document.write( !(x==y) + "<br>");
    </script>

관련글 더보기