1 + '2' // → "12"
`1 + 1 = ${1 + 1} // → "1 + 1 = 2"
1 - '1' // -> 0
1 * '10' // -> 10
1 / 'one' // -> NaN
'1' > 0 // -> true
'abc' > 0 // -> false
// 문자열 타입
+'' // 0
+'0' // 0
+'1' // 1
+'string' // NaN
// 불리언 타입
+true // 1
+false // 0
// null 타입
+null // 0
// undefined 타입
+undefined // NaN
// 심벌 타입
+Symbol() // TypeError : Cannnot convert a Symbol value to a number
// 객체 타입
+{} // NaN
+[] // 0
+[10, 20] // NaN
+(function(){}) // NaN
if ("") console.log("a");
if (true) console.log("b");
if (0) console.log("c");
if ("str") console.log("d");
if (null) console.log("e");
// b d
: 표현식을 평가하는 도중에 평가 결과가 확정된 경우 나머지 평가 과정을 생략하는 것
논리합(||) 또는 논리곱(&&) 연산자 표현식은 언제나 2개의 피연산자 중 어느 한쪽으로 평가됨
// || 연산자
'Cat' || 'Dog' // 'Cat'
false || 'Dog' // 'Dog'
'Cat' || false // 'Cat'
// && 연산자
'Cat' && 'Dog' // 'Dog'
false && 'Dog' // false
'Cat' && false // false
| 단축 평가 표현식 | 평가 결과 |
|---|---|
| true || anything | true |
| false || anyting | anything |
| true && anything | anything |
| false && anything | false |
?. = 좌항의 피연산자가 null 또는 undefined인 경우 undefined를 반환하고, 그렇지 않으면 우항의 프로퍼티 참조를 이어감
var elem = null;
// elem이 null 또는 undefined이면 undefined를 반환
// 그렇지 않으면 우항의 프로퍼티 참조를 이어감
var value = elem?.value;
console.log(value); // -> undefined
var str = '';
// 문자열의 길이(length)를 참조
// 이때 좌항 피연산자가 false로 평가되는 Falsy 값이어도
// null 또는 undefined가 아니면 우항의 프로퍼티 참조를 이어감
var length = str?.length;
console.log(length); // -> 0
?? = 좌항의 피연산자가 null 또는 undefined인 경우 우항의 피연산자를 반환하고, 그렇지 않으면 좌항의 피연산자를 반환
// 좌항의 피연산자가 null 또는 undefined이면 우항의 피연산자를 반환
// 그렇지 않으면 좌항의 피연산자 반환
var foo = null ?? 'default string';
console.log(foo); // "default string"