function three(){
console.log("i love js");
throw Error("this is an error");
}
function two(){
three();
}
function one(){
two();
}
function zero(){
one();
}
zero();
위 코드를 예로 들면 스택에 밑에서부터 zero()-one()-two()-three() 순서대로 쌓이고 three()가 실행되면서 console.log, Error()가 실행된 후 위에서부터(LIFO) 함수가 pop된다.
==
의 경우 타입이 달라도 타입 변환된 값이 같으면 true를 리턴하므로 사용에 주의가 필요하다. 되도록이면 ===
을 사용하는 것이 바람직하다.이하 코드 예제의 출처는 https://poiemaweb.com/js-type-coercion
+
연산자는 피연산자 중 하나 이상이 문자열이라면 문자열 연결 연산자로 동작한다.// 숫자 타입
0 + '' // "0"
-0 + '' // "0"
1 + '' // "1"
-1 + '' // "-1"
NaN + '' // "NaN"
Infinity + '' // "Infinity"
-Infinity + '' // "-Infinity"
// 불리언 타입
true + '' // "true"
false + '' // "false"
// null 타입
null + '' // "null"
// undefined 타입
undefined + '' // "undefined"
// 심볼 타입
(Symbol()) + '' // TypeError: Cannot convert a Symbol value to a string
// 객체 타입
({}) + '' // "[object Object]"
Math + '' // "[object Math]"
[] + '' // ""
[10, 20] + '' // "10,20"
(function(){}) + '' // "function(){}"
Array + '' // "function Array() { [native code] }"
``
+
를 제외한 산술연산자는 피연산자 중 숫자 타입이 아닌 피연산자를 숫자 타입으로 타입 변환한다. 이 때 피연산자를 숫자 타입으로 변환할 수 없을 경우 NaN을 반환한다. +
연산자는 숫자 타입이 아닌 피연산자가 하나만 존재할 때는 숫자 타입으로 타입 변환한다.// 문자열 타입
+'' // 0
+'0' // 0
+'1' // 1
+'string' // NaN
// 불리언 타입
+true // 1
+false // 0
// null 타입
+null // 0
// undefined 타입
+undefined // NaN
// 심볼 타입
+Symbol() // TypeError: Cannot convert a Symbol value to a number
// 객체 타입
+{} // NaN
+[] // 0
+[10, 20] // NaN
+(function(){}) // NaN
위 값 외에는 모두 true로 변환된다.