본문 바로가기
Java Script

Java Script-연산자

by Hrin_0820 2020. 9. 9.

1. 산술 연산자

▶ 이항 산술 연산자

이항 산술 연산자의 종류 설명
+ 덧셈
문자와 문자, 문자와 변수 등을 연결할 때 사용
- 뺄셈
* 곱셈
/ 나눗셈
% 나머지
'반복적인 숫자 구간의 패턴', 조건문을 위한 '짝·홀수 판단, 배수 판단' 등의 식을 만드는데 생성
    // +, -, *, /
    var num1 = 20;
    var num2 = 10;
    var score1 = num1 + num2;
    var score2 = num1 - num2;
    var score3 = num1 * num2;
    var score4 = num1 / num2;
    console.log(score1+', '+score2+', '+score3+', '+score4);

    // %
    var even0dd = 2;
    var result = even0dd % 2;
    var comment;
    if(result === 0){
        comment = even0dd + '는 "짝수" 입니다.';
    } else {
        comment = even0dd + '는 "홀수" 입니다.';
    }
    console.log(comment);

출력 이미지

▶ 단항 산술 연산자

단항 산술 연산자의 종류 설명
++ 1씩 증가
-- 1씩 감소
var x = 5, result;

// 선대입 후증가 (Postfix increment operator)
result = x++;
console.log(result, x); // 5 6

// 선증가 후대입 (Prefix increment operator)
result = ++x;
console.log(result, x); // 7 7

// 선대입 후감소 (Postfix decrement operator)
result = x--;
console.log(result, x); // 7 6

// 선감소 후대입 (Prefix decrement operator)
result = --x;
console.log(result, x); // 5 5

출력 이미지

2. 대입 연산자 (할당 연산자)

대입 연산자 종류 사례 동일 표현
= x=y x = y
'같다'가 아닌 변수에 대입(저장)을 의미
+= x+=y x = x + y
기존 데이터에 새로운 데이터를 연결하여 누적
-= x -= y x = x - y
*= x *= y x = x * y
/= x /= y x = x / y
%= x %= y x = x % y
        var x;

        x = 10; 
        console.log(x);
        x += 5; 
        console.log(x);
        x -= 5;  
        console.log(x);
        x *= 5;  
        console.log(x);
        x /= 5;  
        console.log(x);
        x %= 5; 
        console.log(x);

        var str = 'My name is ';
        str += 'Lee'; 
        console.log(str);

출력 이미지

3. 비교 연산자

비교연산자는 값을 비교하여 결과 값을 Boolean(논리) 값인 'true'나 'false'로 반환한다.

▶ 동등 / 일치 비교 연산자

비교 연산자 의미 사례 설명
== 동등 비교 x==y x와 y의 값이 같음
=== 일치 비교 x===y x와 y의 값과 데이터 타입이 같음
!= 부등 비교 x!=y x와 y의 값이 다름
!== 불일치 비교 x!==y x와 y의 값과 데이터 타입이 다름

▶ 대소 관계 비교 연산자

대소 관계 비교 연산자 사례 설명
> x>y x가 y보다 크다
< x<y x가 y보다 작다
>= x>=y x가 y보다 같거나 크다
<= x<=y x가 y보다 같거나 작다
    var a1 = (5 > 2);
    console.log(a1);
    var a2 = (5 < 2);
    console.log(a2);
    var a3 = (5 == 5);
    console.log(a3);
    var a4 = (5 == '5');
    console.log(a4);
    var a5 = (5 === '5');
    console.log(a5);
    var a6 = (5 != '5');
    console.log(a6);
    var a7 = (5 !== '5');
    console.log(a7);

출력 이미지

4. 논리 연산자

논리 연산자 종류 설명
&& 논리곱(AND)
true && true = true
false && true = false
true && false = false
false && false = false
|| 논리합(OR)
true || true = true
false || true = true
true || false = true
false || false = false
! 부정(NOT)
!a, a가 true면 false, false면 true로 바꾸어 준다.
    var logic1, logic2, logic3, logic4, logic5;
    logic1 = (3>2) && (5>3);
    console.log(logic1);
    logic2 = (3<2) && (5>3);
    console.log(logic2);
    logic3 = (3>2) || (5>3);
    console.log(logic3);
    logic4 = (3<2) || (5<3);
    console.log(logic4);
    logic5 = !(3<2);
    console.log(logic5);
    logic6 = !(3>2);
    console.log(logic6);

출력 이미지

5. 삼항 연산자

조건식의 결과(true, false)에 따라 결과 값을 다르게 나오게 해주는 연산자.

삼항 연산자의 형식

var num1 = 10;
var num2 = -10;
console.log(num1 > 0 ? '양수' : '음수');  //true
console.log(num2 > 0 ? '양수' : '음수');  //false

출력 이미지

'Java Script' 카테고리의 다른 글

Java Script-함수  (0) 2020.09.17
Java Script-제어문  (0) 2020.09.10
Java Script-변수와 상수, 데이터 타입  (0) 2020.09.09
Java Script-기본 문법  (0) 2020.09.07
Java Script-기본 형식 및 적용 방법  (0) 2020.09.07

댓글