article thumbnail image
Published 2022. 2. 11. 06:04

1. 산술 연산자 

console.log(1 + 2)
console.log(1 - 2)
console.log(1 * 2)
console.log(1 / 2)
console.log(1 % 2) //나머지..

2. 할당 연산자

let a = 2
// a = a + 1
a += 1 //위쪽에 적은 코드와 같은 코드, (산술 연산자에도 당연하게 다 사용가능)
console.log(a)

3. 비교 연산자

예제1)
const a = 1
const b = 1

console.log(a === b) //true

function isEqual(x, y) {
  return x === y
}

console.log(isEqual(1, 1)) //true
console.log(isEqual(1, '2')) //false (문자데이터가 있어서)

예제2)
const a = 1
const b = 1

console.log(a !== b) //불일치 연산자(느낌표가 있으면 반대로 값이 나옴)

예제3)
const a = 1
const b = 1

console.log(a >= b) //크거나 같다.(이퀄괄호는 꺽쇠보다 뒤로!)

4. 논리 연산자

const a = 1 === 1
const b ='ab' === 'ab'
const c = true

console.log(a && b && c) // (and연산자)
//abc가 모두 true여야한 true가 나옴 (하나라도 false면 안됨)
console.log(a || b || c) // (or연산자)
//abc중 하나이상의 값이 true면 true를 반환
console.log(!a) //느낌표는 (not연산자)
//느낌표는 데이터의 반대값이 나옴

5. 삼항 연산자

const a = 1 < 2 //true

if () {
  console.log('참')
}else {
  console.log('거짓')
}
//실행할 코드가 단순하면 if else조건문을 사용하면됨 
// but 더 간단하게 만들 수 있음

//세개의 항을 가지고 있어서 삼항 연산자
console.log(a ? '참' : '거짓')
//a 값은 true?false? true면 첫번째 false면 두번째 값으로 반환

'개발이야기 > JS' 카테고리의 다른 글

[JS] 반복문 (for)  (0) 2022.02.11
[JS] 조건문(2) switch  (0) 2022.02.11
[JS] DOM API  (0) 2022.01.31
[JS] 조건문 (if, else)  (0) 2022.01.31
[JS] 함수 (function)  (0) 2022.01.31
복사했습니다!