‘skill-check’ JS 퀴즈
Source: Dev.to
Question 1: Type coercion
What does the following code output to the console?
console.log(0 == '0');
console.log(0 === '0');
Answer: true, then false
Explanation: “JavaScript Type Coercion”을 검색하면 ==는 타입 변환을 수행하고 ===는 수행하지 않는 이유를 이해할 수 있습니다.
Question 2: Arrow function this
In the snippet below, what will be logged?
const user = {
name: 'Alex',
greet: () => {
console.log(`Hi, I'm ${this.name}`);
}
};
user.greet();
Answer: Hi, I'm undefined (or an empty string in some environments)
Explanation: 화살표 함수는 주변 렉시컬 스코프(전역 객체 또는 모듈)에서 this를 상속받으며, 정의된 객체에서 this를 가져오지 않습니다. user.name에 접근하려면 일반 함수 표현식을 사용하세요.
Question 3: var vs let in asynchronous loops
What is the result of this execution?
for (var i = 0; i console.log(i), 1);
}
Answer: 3, 3, 3
Explanation: var는 함수 스코프이므로 루프가 setTimeout 콜백이 실행되기 전에 끝나고 i는 3이 됩니다. var 대신 let을 사용하면 각 반복마다 새로운 바인딩이 생성되어 0, 1, 2가 출력됩니다.
Question 4: Array reference behavior
What happens to list2?
let list1 = [1, 2, 3];
let list2 = list1;
list1.push(4);
console.log(list2.length);
Answer: 4
Explanation: 배열은 참조에 의해 전달되는 객체입니다. list2는 복사본이 아니라 list1과 같은 내부 배열을 가리킵니다.
Question 5: typeof null
What is typeof null?
Answer: "object"
Explanation: 이는 JavaScript 최초 버전에서 발생한 오래된 특이점이며, 하위 호환성을 위해 현재까지 유지되고 있습니다.