본문 바로가기
JavaScript

[JavaScript]연산자

by eddypark 2023. 9. 26.

(1) 산술 연산자

- 사칙 연산을 다루는 연산자이다.

- 결합 방향은 왼쪽에서 오른쪽으로 진행된다.

- 덧셈(+), 뺄셈(-), 곱셈(*), 나눗셈(/), 나머지(%)

(2) 대입 연산자

- 변수에 값을 대입할 때 쓰이는 이항 연산자이다.

- +=, -=, *=, /=, %=

var x = 10;
console.log(x+=5);  // x = x + 5  -> 15
console.log(x-=5);  // x = x - 5  -> 10
console.log(x*=5);  // x = x * 5  -> 50
console.log(x/=5);  // x = x / 5  -> 10
console.log(x%=5);  // x = x % 5  -> 0

(3) 증감 연산자

- 1씩 증가 혹은 감소시킬 때 사용하는 연산자

- ++변수, 변수++, --변수, 변수--

var x = 10;
console.log(x++);  // console 진행(10) -> x값 증가(11)
console.log(++x);  // x값 증가(12) -> console 진행(12)
console.log(x--);  // console 진행(12) -> x값 감소(11)
console.log(--x);  // x값 감소(10) -> console 진행(10)
var a = 10;
var b = a -- + 5 + --a; // a+5 -> a-- -> --a -> +
console.log(a, b); // a= 8, b= 23

(4) 비교 연산자

- 피 연산자의 값의 크기를 판단하여 true와 false로 반환한다.

- ==, ===, !=, !==, >, >=, <, <=

var x = 10, y = 5;
var a = '10';
var i = null;
var j;
console.log(x == y);  // 값이 같은가? false
console.log(x === y); // 타입과 값이 모두 같은가?            false 
console.log(x != y);  // 값이 다른가?                        true
console.log(x !== y); // 타입과 값이 모두 다른가?            true
console.log(x > y);   // x의 값이 y의 값보다 큰가?           true
console.log(x >= y);  // x의 값이 y의 값보다 큰거나 같은가?  true
console.log(x < y);   // x의 값이 y의 값보다 작은가?         false
console.log(x <= y);  // x의 값이 y의 값보다 작거나 같은가?  false

console.log(x == a);  // x의 값이 a의 값이 같은가?           true(a의 타입변환)
console.log(x === a); // x타입과 값이 모두 같은가?           false

console.log(i == j);  // true
console.log(i === j); // false

(5) 논리 연산자

- 주어진 논리식을 판단하여 true와 false로 반환한다.

- &&(AND), ||(OR), !(NOT)

var a = true, b = true;
console.log(a && b) // true
console.log(a || b) // true
console.log(!a)     // false

b = false
console.log(a && b) // false
console.log(a || b) // true
console.log(!b)     // true

a = false
console.log(a && b) // false
console.log(a || b) // false
console.log(!a)     // true

(6) 비트 연산자

- 비트 단위로 논리 연산 수행하는 연산자이다.

- &(비트 AND), |(비트 OR), ^(비트 XOR), ~(비트 NOT), <<(LeftShift), >>(RightShift)

const a = 5;         // 00000000000000000000000000000101
const b = 3;         // 00000000000000000000000000000011

console.log(a & b);  // 00000000000000000000000000000001  -> 1
console.log(a | b);  // 00000000000000000000000000000111  -> 7
console.log(a ^ b);  // 00000000000000000000000000000110  -> 6
console.log(~a);     // 11111111111111111111111111111010  -> -6
console.log(a << b); // 00000000000000000000000000101000  -> 20
console.log(a >> b); // 00000000000000000000000000000000  ->0

(7) 문자열 결합 연산자

- 피연산자가 하나라도 문자열이라면 문자열 결합 수행하는 연산자이다.

console.log('10' + 10);                      // 1010
console.log("좋은 " + "하루 되세요!");       // 좋은 하루 되세요!

(8) 삼항 연산자

- 피연산자를 세 개 가지는 조건 연산자이다.

- 표현식 ? 반환값1 : 반환값2 참이면 반환값 1을 반환, 거짓이면 반환값 2를 반환한다.

var x = 3, y = 5;
var result = (x > y) ? x : y; 
console.log(result);  // 5

(9) typeof 연산자

- 피연산자의 타입을 반환한다.

console.log(typeof "문자열")   // string
console.log(typeof 10)         // number
console.log(typeof NaN)        // number
console.log(typeof false)      // boolean
console.log(typeof undefined)  // undefined
console.log(typeof new Date()) // object
console.log(typeof null)       // object

'JavaScript' 카테고리의 다른 글

[JavaScript] 객체  (0) 2023.10.04
[JavaScript] 타입변환과 단축평가  (0) 2023.09.27
[JavaScript] 제어문  (0) 2023.09.27
[JavaScript]데이터 타입  (0) 2023.09.25
[JavaScript] 기본 문법  (0) 2023.09.25