[개념] 연산자

2021. 6. 10. 22:50개발공부/자바스크립트

let count = 1

// count라는 변수에 1을 대입

const preIncrement = ++count

// count = count + 1

// count라는 변수에 count+1을 할당

// const preIncrement = count

// preIncrement라는 변수에 count할당

let count = 1
const preCount = ++count
console.log(`count: ${count}, preCount: ${preCount}`)

>> count: 2, preCount: 2

 

그렇다면 증감연산자를 뒤에 붙인다면?

let count = 1

const postIncrement = count++

// const postIncrement = count

// postIncrement라는 변수에 자기 자신의 값을 먼저 할당

// count = count + 1

// 그 후 1을 더해서 재할당

let count = 1
const preCount = count++
console.log(`count: ${count}, postCount: ${postCount}`)

>> count: 2, postCount: 1

여기서 count에 const가 아닌 let을 사용하는 이유?

-> 증감연산자를 이용해 count의 값을 계속 증가시키고 다시 count에 할당하고 있기때문에

 

대입연산자

totalPrice += firstPrice

// totalPrice  = totalPrice + firstPrice

 

totalPrice -= firstPrice

// totalPrice = totalPrice - firstPrice

 

비교연산자

이미 알고있는 <, <=, >, >=

 

논리연산자

||(or), &&(and), !(not)

let isTest = true
let isNotTest = true

console.log(isTest && isNotTest)
// true && true = true
console.log(isTest || isNotTest)
// true || true = true

isTest = false
console.log(isTest && isNotTest)
// false && true = false
console.log(isTest || isNotTest)
// false || true = true

isNotTest = false
console.log(!isNotTest)
// !false = true

 

일치연산자

두 값이 일치하는지 비교

 

=== (Strict Equality Operators)

두 값의 데이터타입과 값 자체가 정확히 일치해야만 true

 

==

비교하는 두 값의 데이터타입이 다를경우 자동으로 변환해줌

하지만 실수 유발 가능성이 커서 실무에선 거의 안씀

console.log(1 === "1")
>> fales

console.log(1 == "1")
>> true

'개발공부 > 자바스크립트' 카테고리의 다른 글

[코어 자바스크립트] 1. 데이터 타입  (0) 2021.06.19
[개념] 배열  (0) 2021.06.11
[개념] 클래스와 객체, 메소드, 객체 리터럴  (0) 2021.06.11
[개념] 함수  (0) 2021.06.11
[개념] 조건문, 반복문  (0) 2021.06.11