연산자
산술연산자
산술 연산자에는 산수 시간에 배운 더하기(+), 빼기(-), 곱하기(*), 나누기(/), 나머지(%)가 있습니다. 산술 연산자로 연산을 하기 위해서는 연산 대상 데이터가 반드시 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>
문자 결합 연산
문자 결합 연산자느 피연산자(연산 대상 데이터)가 문자형 데이터입니다. 여러 개의 문자를 하나의 문자형 데이터로 결합할 때 사용합니다.
다음과 같이 더하기에 피연자로 문자형 데이터가 한개라도 포함되어 있으면 다른 피연자의 데이터는 자동으로 문자형 데이터로 형 변환되고 문자 결합이 이루어져 하나의 문자형 데이터를 반환합니다.
<script>
var t1 = "학교종이";
var t2 = "땡땡땡";
var t3 = 8282;
var t4 = "어서모이자";
document.write(t1+t2+t3+t4)
</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>");
</script>
예제
function func4(){
let i=10, j=10; k=30;
i /= j; // i = i / j //i=1
j -= i; // j = j - i //j=9
k %= j; // k = k % j //k=3
document.write(i);
document.write(j);
document.write(k);
}
func4();
복합 대입 연산
<script>
var str = "<table border= '1'>";
str += "<tr>"
str += "<td>1</td>"
str += "<td>2</td>"
str += "</tr>"
str += "</table>";
document.write(str);
</script>
표 만들기
let num = 1;
let table = "<table border='1'>"
for(let i =1; i<=10; i++){
table += "<tr>";
for(let j=1; j<=7; j++){
table += "<td>"+num+"</td>";
num++;
}
table += "</tr>";
}
document.write(table);
증감연산자
증감 연산자에는 숫자형 데이터를 1식 증가시키는 증가 연산자(++)와 반대로 1씩 감소시키는 감소 연산자(--)가 있습니다. 증감 연산자는 앞에서 배운 연산자와는 달리 피연산자가 한 개만 필요한 단항 연산자입니다. 증감 연산자는 변수의 어느 위치에 오는가에 따라 결괏값이 달라집니다.
let num1 = 10; //전역변수 num1에 숫자 10을 할당
let num2 = 20; //전역변수 num2에 숫자 20을 할당
let num3 = 20; //전역변수 num3에 숫자 20을 할당
let rusult;
document.write(num1, "<br>")
num1--;
document.write(num1, "<br>")
num1++;
document.write(num1, "<br>")
rusult = num2++;
document.write(rusult, "<br>")
document.write(num2, "<br>")
rusult = ++num3;
document.write(rusult, "<br>")
document.write(num2, "<br>")
//10
//9
//10
//20
//21
//22
//22
기본형 ① 변수의 값을 1만큼 감소시킵니다.
변수--; 또는 --변수;
② 변수의 값을 1만큼 증가시킵니다.
변수++; 또는 ++변수;
1 먼저 a(B의 값을 1만큼 증가)가 실행되고, b(증가된 B의 값을 A에 대입)가 실행됩니다.
var A(b) = ++B(a)
2 먼저 a(B의 값을 A에 대입)가 실행되고, b(B의 값을 1만큼 증가)가 실행됩니다.
var A =B++;(b)
(a)
function fun1(){
let result, a = 100, b = 200, c = 300;
result = a< b ? b++ :--c;
document.write(result);
}
fun1();
function func2(){
let hap, j, k, l;
j = k = l = 0;
hap = ++j + k++ + ++l;
document.write(hap);
}
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>");
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>");
//false
//true
//true
//true
//true
//false
논리연산자
논리 연산자에는 ||(or), &&(and), !(not)이 있으며, 논리 연산자는 피연산자가 논리형 데이터인 true 또는 false로 결괏값을 반환합니다.|| (or) 연산자는 피연산자 중 하나만 true이면 true라는 결괏값을 반환합니다. 하지만 &&(and) 연산자는 피연산자 중 하나만 false이면 false라는 결괏값을 반환합니다. !(not)은 리 부정 연산자로, 피연산자가 true이면 false라는 반대의 결괏값을 반환합니다.
논리 연산자의 종류
종류
예시
설명
&&
X&&Y
둘다 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;
document.write(result, "<br>")
result = a > b || b >= m || m <= n;
document.write(result, "<br>")
result = a <= b && b >= m && m <= n;
document.write(result, "<br>")
result = a <= b && b <= m && m <= n;
document.write(result, "<br>")
result = !(a>b);
document.write(result, "<br>")
//false
//true
//false
//true
//true
document.write(false || false)
document.write("<br>")
document.write(false || true)
document.write("<br>")
document.write(true || true)
document.write("<br>")
document.write(false && false)
document.write("<br>")
document.write(false && true)
document.write("<br>")
document.write(true && true)
//false
//true
//true
//false
//false
//true
연산자 우선순위
일반적인 산수를 연산 할 때처럼 우선순위가 있습니다.
( )
단항 연산자( --, ++, ! )
산술 연산자( *, /, %, +, - )
비교 연산자( >,>=,<.<=,==,===,!==,!= )
논리 연산자( &&, II )
대입(복합 대입) 연산자( =, +=, -=, *=, /=, %= )
Last updated
Was this helpful?