5 * 4
'My name is ' + 'Lee'
color = 'red'
3 > 5
true && false
typeof 'Hi'
단항 산술 연산자
var x = 1;
x++;
console.log(x);
X--;
console.log(x);
var x = 5, result;
result = x++;
console.log(result, x);
result = ++x;
console.log(result, x);
result = x-- ;
console.log(result, x);
result = --x;
console.log(result, x);
var x = '1';
console.log(+x);
console.log(x);
x = true;
console.log(+x);
console.log(x);
x = false;
console.log(+x);
console.log(x);
x = 'Hello';
console.log(+x);
console.log(x);
-(-10);
-'10';
-true
'Hello';
문자열 연결 연산자
- ‘+’ 연산자는 피연산자 중 하나 이상이 문자열이면 문자열 연결 연산자로 작동
- 개발자 의도와 상관없이 JS 엔진에 의해 암묵적인 타입 자동 변환이 일어나기에 명시적 타입변환 권장
- 이를 암묵적 타입 변환(implicit coercion), 타입 강제 변환(type, coercion)
'1' + 2;
1 + 2'; // 12'
1 + 2;
1 + true;
1 + false;
1 + null;
+undefined;
1 + undefined;
할당 연산자(assignment operator)
var x;
× = 10;
console.log(x);
x += 5;
console.log(x);
x -= 5;
console. log(x);
x*= 5;
console.log(x);
х /= 5;
console.log(x);
x %= 5;
console. log(x);
var str = 'My name is ';
str += 'Lee';
console.log(str);
비교 연산자(comparison operation)
- 동등/일치 비교가 있고 동등 비교는 느슨한 비교, 일치 비교는 엄격한 비교를 한다
- 왠만하면 타입 체크까지 하는 ‘===’ 권장
- ‘==’ 의 경우 타입 변환이 일어남, 타입 일치하지 않아도 true
- ‘===’ 의 경우 타입 변환이 일어나지 않으며 값과 타입이 일치해야 true
| 비교 연산자 | 의미 | 사례 | 설명 | 부수 효과 |
|---|
| == | 동등 비교 | x == y | x와 y의 값이 같음 | × |
| === | 일치 비교 | x === y | x와 y의 값과 타입이 같음 | × |
| != | 부동등 비교 | x != y | x와 y의 값이 다름 | × |
| !== | 불일치 비교 | x !== y | x와 y의 값과 타입이 다름 | × |
'' == '0'
0 == ''
0 == '0'
false == 'false'
false == '0'
false == undefined
false == null
null == undefined
' \t\r\n ' == 0
var a = {}
var b = {}
a == b
a === b
var c = [];
var d = [];
c == d
c === d
var a = "string"
var b = new String("string")
a == b
a === b
- 0, NaN은 주의하기
- NaN은 자신과 일치 하지 않은 유일한 값
isNaN(NaN);
isNaN(10);
isNaN(1 + undefined);
- 0은 2종류, 양의 0과 음의 0이 있지만 모든 비교는 true
0 == -0;
0 === -0;
-0 === +0;
Object.is(-0, +0);
NaN === NaN;
Object.is(NaN, NaN);
삼항 조건 연산자(ternary operator)
let age = 18;
let result = age >= 18 ? 'Adult' : 'Minor';
console.log(result);
전개연산자
const fruits = ['apple', 'banana', 'orange']
const res = ...fruits