💻
javascript
  • 자바스크립트 시작하기
  • 자바스크립트 기초 문법
  • 변수
  • 배열
  • 객체
  • 연산자
  • 조건문
    • if문
    • if ~else문
    • 다중 if문
    • 중첩 if문
    • switch문
    • 삼항 연산자
  • 반복문
    • while문
    • do while문
    • for문
    • 중첩 for문
    • break문
    • continue문
  • 함수
    • 선언적 함수
    • 익명함수
    • 매개변수가 있는 함수
    • arguments 함수
    • 리턴값이 있는 함수
    • 재귀함수
    • 콜백함수
    • 내부함수(스코프)
    • 객체 생성자 함수
    • 프로토타입 함수
    • 화살표 함수
    • 클래스
    • Promise
    • 템플릿 리터럴
  • 내장객체
    • String 객체
      • split()
      • join()
    • Number 객체
    • Date 객체
    • Array 객체
    • Math 객체
    • 정규표현 객체
  • 브라우저 객체
    • window 객체
    • navigator 객체
    • screen 객체
    • history 객체
    • location 객체
  • 문서객체
  • 이벤트
Powered by GitBook
On this page
  • 산술연산자
  • 대입 연산자
  • 표만들기
  • 증감연산자
  • 비교연산자
  • 논리연산자
  • 연산자 우선순위

Was this helpful?

연산자

산술연산자

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

//result 값을 400을 입력하였다 하더라도 그 뒤 연산자로 인하여 값이 변한다.

대입 연산자

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

종류

풀이

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 />");    
</script>
function func4(){
    let i = 10, j = 10, k = 30;
    i /= j;
    j -= i;
    k %= j;
    document.write(i);
    document.write(j);
    document.write(k);
}
func4();
//1
//9
//3

표만들기

<script>
    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</td>";
    str += "<td>34</td>";
    str += "<td>916</td>";
    str += "<td>-</td>";
    str += "</tr>";
    str += "</table>";
    
    document.write(str);
</script>

증감연산자

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

기본형

변수의 값을 1만큼 감소시킵니다. 변수 --; 또는 --변수; 변수의 값을 1만큼 증가시킵니다. 변수 ++; 또는 ++변수;

let A = ++B //++B(B의 값을 1만큼 증가)가 실행되고, A(증가된 B의 값을 A에 대입)가 실행됩니다.
let A = B++ //A(B의 값을 A에 대입)가 실행되고, B++(B의 값을 1만큰 증가)가 실행됩니다.
let num1 = 10; //전역변수 num1에 숫자 10을 할당
let num2 = 20; //전역변수 num2에 숫자 20을 할당
let num3 = 20; //전역변수 num2에 숫자 20을 할당
let result;

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

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

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

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

result = ++num3;
document.write(result,"<br />")    //21
document.write(num3,"<br />")      //21
function fun1(){
    let result, a = 100, b = 200, c = 300;
    result = a < b ? b++ : --c;
    document.write(result);
}
fun1()
//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

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

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

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

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

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

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

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

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

논리연산자

논리 연산자에는 ||(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);            //false

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

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

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

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

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

연산자 우선순위

일반적인 산수를 연산할 때처럼 연산자에도 우선순위가 있습니다.

연산자들 우선순위

  1. ( )

  2. 단항 연산자(- -, ++, !)

  3. 산술 연산자(*, /, %, +, -)

  4. 비교 연산자(>, >=, <, <=, ==, ===, !==, !=

  5. 논리 연산자(&&, ll)

  6. 대입(복합 대입) 연산자(=, +=, -=, *=, /=, %=)

Previous객체Next조건문

Last updated 4 years ago

Was this helpful?